<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>python常用函数 | kiloGrand</title><meta name="keywords" content="python"><meta name="author" content="kiloGrand"><meta name="copyright" content="kiloGrand"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="常用函数">
<meta property="og:type" content="article">
<meta property="og:title" content="python常用函数">
<meta property="og:url" content="https://kilogrand.gitee.io/2021/08/19/python-useful-function/index.html">
<meta property="og:site_name" content="kiloGrand">
<meta property="og:description" content="常用函数">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kilogrand.gitee.io/img/cover.png">
<meta property="article:published_time" content="2021-08-19T01:41:10.000Z">
<meta property="article:modified_time" content="2022-08-05T02:33:41.010Z">
<meta property="article:author" content="kiloGrand">
<meta property="article:tag" content="python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kilogrand.gitee.io/img/cover.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kilogrand.gitee.io/2021/08/19/python-useful-function/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'python常用函数',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-05 10:33:41'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/profile.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kiloGrand</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">python常用函数</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-08-19T01:41:10.000Z" title="发表于 2021-08-19 09:41:10">2021-08-19</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-05T02:33:41.010Z" title="更新于 2022-08-05 10:33:41">2022-08-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%85%B6%E4%BB%96/">其他</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">19.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>95分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="python常用函数"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="1-生成6位数字随机验证码"><a href="#1-生成6位数字随机验证码" class="headerlink" title="1. 生成6位数字随机验证码"></a>1. 生成6位数字随机验证码</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">import</span> string</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">num_code</span>(<span class="params">length=<span class="number">6</span></span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    生成长度为length的数字随机验证码</span></span><br><span class="line"><span class="string">    :param length: 验证码长度</span></span><br><span class="line"><span class="string">    :return: 验证码</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;&#x27;</span>.join(random.choice(string.digits) <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">0</span>, length))</span><br></pre></td></tr></table></figure>
<h2 id="2-md5加密"><a href="#2-md5加密" class="headerlink" title="2.md5加密"></a>2.md5加密</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span>  hashlib</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># md5加密</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">md5_encrypt</span>(<span class="params">en_str</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    使用md5二次加密生成32位的字符串</span></span><br><span class="line"><span class="string">    :param en_str: 需要加密的字符串</span></span><br><span class="line"><span class="string">    :return: 加密后的字符串</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">   </span><br><span class="line">    md5 = hashlib.md5()  <span class="comment"># 使用MD5加密模式</span></span><br><span class="line">    md5.update(en_str.encode(<span class="string">&#x27;utf-8&#x27;</span>))  <span class="comment"># 将参数字符串传入</span></span><br><span class="line">    md5.update(md5.hexdigest().encode(<span class="string">&#x27;utf-8&#x27;</span>))  <span class="comment"># md5二次加密</span></span><br><span class="line">    <span class="keyword">return</span> md5.hexdigest()</span><br></pre></td></tr></table></figure>
<h2 id="3-生成唯一token"><a href="#3-生成唯一token" class="headerlink" title="3. 生成唯一token"></a>3. 生成唯一token</h2><pre><code>import uuid
import  hashlib

def only_token():
    &quot;&quot;&quot;
    使用md5加密uuid生成唯一的32位token
    :return: 加密后的字符串
    &quot;&quot;&quot;
md5 = hashlib.md5()  # 使用MD5加密模式
md5.update(str(uuid.uuid1()).encode(&#39;utf-8&#39;))  
return md5.hexdigest()
</code></pre><h2 id="4、发送手机验证码"><a href="#4、发送手机验证码" class="headerlink" title="4、发送手机验证码"></a>4、发送手机验证码</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">#验证码管理表</span><br><span class="line">class AuthCode(models.Model):</span><br><span class="line">    name = models.CharField(max_length=10,default=None, null=True, blank=True,verbose_name=&#x27;姓名&#x27;)</span><br><span class="line">    phone = models.CharField(max_length=11, unique=True, verbose_name=&#x27;手机号&#x27;)</span><br><span class="line">    code = models.CharField(max_length=6,verbose_name=&#x27;验证码&#x27;)</span><br><span class="line">    purpose = models.IntegerField(default=0,verbose_name=&#x27;用途:0-&gt;注册验证 1-&gt;找回密码 2-&gt;其它&#x27;)</span><br><span class="line">    sendNum = models.IntegerField(default=0,verbose_name=&#x27;发送次数&#x27;)</span><br><span class="line">    isCanGet = models.BooleanField(default=0,verbose_name=&#x27;0-&gt;可以获取,1-&gt;不可以获取&#x27;)</span><br><span class="line">    recentlySendTime = models.DateTimeField(auto_now_add=True,verbose_name=&#x27;最近一次发送时间&#x27;)</span><br><span class="line">    creation_time = models.DateTimeField(auto_now=True, verbose_name=&#x27;创建时间&#x27;)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        verbose_name = &#x27;手机验证码&#x27;</span><br><span class="line">        verbose_name_plural = verbose_name</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="实现逻辑"><a href="#实现逻辑" class="headerlink" title="实现逻辑"></a>实现逻辑</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">import http.client</span><br><span class="line">import urllib</span><br><span class="line"># 使用互亿无线</span><br><span class="line">host = &quot;106.ihuyi.com&quot;</span><br><span class="line">sms_send_uri = &quot;/webservice/sms.php?method=Submit&quot;</span><br><span class="line"></span><br><span class="line"># 查看用户名 登录用户中心-&gt;验证码通知短信&gt;产品总览-&gt;API接口信息-&gt;APIID</span><br><span class="line">account = &quot;你的用户名&quot;</span><br><span class="line"># 查看密码 登录用户中心-&gt;验证码通知短信&gt;产品总览-&gt;API接口信息-&gt;APIKEY</span><br><span class="line">password = &quot;你的密码&quot;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def send_sms(text, mobile):</span><br><span class="line">    text = f&quot;您的验证码是：&#123;text&#125;。请不要把验证码泄露给其他人。&quot;</span><br><span class="line">    params = urllib.parse.urlencode(</span><br><span class="line">        &#123;&#x27;account&#x27;: account, &#x27;password&#x27;: password, &#x27;content&#x27;: text, &#x27;mobile&#x27;: mobile, &#x27;format&#x27;: &#x27;json&#x27;&#125;)</span><br><span class="line">    headers = &#123;&quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot;, &quot;Accept&quot;: &quot;text/plain&quot;&#125;</span><br><span class="line">    conn = http.client.HTTPConnection(host, port=80, timeout=30)</span><br><span class="line">    conn.request(&quot;POST&quot;, sms_send_uri, params, headers)</span><br><span class="line">    response = conn.getresponse()</span><br><span class="line">    response_str = response.read()</span><br><span class="line">    conn.close()</span><br><span class="line">    return response_str</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == &#x27;__main__&#x27;:</span><br><span class="line">    mobile = &quot;手机号&quot;</span><br><span class="line">    text = &#x27;123122&#x27;</span><br><span class="line"></span><br><span class="line">    print(json.loads(send_sms(text, mobile).decode(&#x27;utf-8&#x27;)))</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="5、生成二维码"><a href="#5、生成二维码" class="headerlink" title="5、生成二维码"></a>5、生成二维码</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">import qrcode</span><br><span class="line">import io</span><br><span class="line"></span><br><span class="line">def maker_qrcode(url):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    生成二维码</span><br><span class="line">    :param url: 需要生成二维码的url</span><br><span class="line">    :return: 返回图片字节流</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    image = qrcode.make(url)  # 创建二维码片</span><br><span class="line"></span><br><span class="line">    buffer = io.BytesIO()</span><br><span class="line">    # 将图片内容丢入容器</span><br><span class="line">    image.save(buffer, &#x27;png&#x27;)</span><br><span class="line">    # 返回容器内的字节</span><br><span class="line">    return buffer.getvalue()</span><br><span class="line">    </span><br><span class="line">或者</span><br><span class="line">from .settings import BASE_DIR</span><br><span class="line"></span><br><span class="line">def create_qrcode(name, url):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    生成机器扫码支付二维码</span><br><span class="line">    :param name: 图片名称</span><br><span class="line">    :param url: 支付路由</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    img = qrcode.make(url, border=0)  # 创建二维码片</span><br><span class="line">    save_path = BASE_DIR + &#x27;/&#x27; + name + &#x27;.png&#x27;</span><br><span class="line">    print(save_path)</span><br><span class="line">    img.save(save_path)</span><br><span class="line">    return img</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="6-微信群发"><a href="#6-微信群发" class="headerlink" title="6.微信群发"></a>6.微信群发</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"># coding=utf8</span><br><span class="line">import itchat, time</span><br><span class="line"></span><br><span class="line">itchat.auto_login(True)</span><br><span class="line"></span><br><span class="line">SINCERE_WISH = u&#x27;祝%s新年快乐！&#x27;</span><br><span class="line"></span><br><span class="line">friendList = itchat.get_friends(update=True)[35:]</span><br><span class="line">count = 0</span><br><span class="line"></span><br><span class="line">for  index,friend in enumerate(friendList):</span><br><span class="line">    print(index,friend[&#x27;DisplayName&#x27;],friend[&#x27;NickName&#x27;])</span><br><span class="line">    itchat.send(SINCERE_WISH % (friend[&#x27;DisplayName&#x27;]</span><br><span class="line">                                or friend[&#x27;NickName&#x27;]), friend[&#x27;UserName&#x27;])</span><br><span class="line">    time.sleep(2)</span><br><span class="line">    print(&#x27;备注名称&#x27;,friend[&#x27;DisplayName&#x27;],&#x27;昵称&#x27;,friend[&#x27;NickName&#x27;],&#x27;用户名&#x27;,friend[&#x27;UserName&#x27;])</span><br><span class="line"></span><br><span class="line">print(&quot;----end----&quot;)</span><br><span class="line"></span><br><span class="line">&quot;&quot;&quot;</span><br><span class="line"># 发送文本</span><br><span class="line">itchat.send(&#x27;Hello, WeChat!&#x27;)</span><br><span class="line"># 发送图片</span><br><span class="line">itchat.send_image(&#x27;my_picture.png&#x27;)</span><br><span class="line"># 发送视频</span><br><span class="line">itchat.send_video(&#x27;my_video.mov&#x27;)</span><br><span class="line"># 发送文件</span><br><span class="line">itchat.send_file(&#x27;my_file.zip&#x27;)</span><br><span class="line"></span><br><span class="line">&quot;&quot;&quot;</span><br></pre></td></tr></table></figure>
<h2 id="7、微信自动回复"><a href="#7、微信自动回复" class="headerlink" title="7、微信自动回复"></a>7、微信自动回复</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"># -*- coding=utf-8 -*-</span><br><span class="line">import requests</span><br><span class="line">import itchat</span><br><span class="line">import random</span><br><span class="line">#图灵机器人</span><br><span class="line">#http://www.tuling123.com/member/robot/1380138/center/frame.jhtml?page=0&amp;child=0获取apikey</span><br><span class="line">KEY = &#x27;你的KEY&#x27;</span><br><span class="line"></span><br><span class="line">def get_response(msg):</span><br><span class="line">    apiUrl = &#x27;http://www.tuling123.com/openapi/api&#x27;</span><br><span class="line">    data = &#123;</span><br><span class="line">        &#x27;key&#x27;    : KEY,</span><br><span class="line">        &#x27;info&#x27;   : msg,</span><br><span class="line">        &#x27;userid&#x27; : &#x27;wechat-robot&#x27;,</span><br><span class="line">    &#125;</span><br><span class="line">    try:</span><br><span class="line">        r = requests.post(apiUrl, data=data).json()</span><br><span class="line">        return r.get(&#x27;text&#x27;)</span><br><span class="line">    except:</span><br><span class="line">        return</span><br><span class="line"></span><br><span class="line">@itchat.msg_register(itchat.content.TEXT)</span><br><span class="line">def tuling_reply(msg):</span><br><span class="line">    defaultReply = &#x27;I received: &#x27; + msg[&#x27;Text&#x27;]</span><br><span class="line">    robots=[&#x27;&#x27;,&#x27;&#x27;,&#x27;&#x27;]</span><br><span class="line">    reply = get_response(msg[&#x27;Text&#x27;])+random.choice(robots)</span><br><span class="line">    return reply or defaultReply</span><br><span class="line"></span><br><span class="line">itchat.auto_login(enableCmdQR=False)</span><br><span class="line">itchat.run()</span><br></pre></td></tr></table></figure>
<h2 id="8、提取Django中model中的字段名变成字典、列表"><a href="#8、提取Django中model中的字段名变成字典、列表" class="headerlink" title="8、提取Django中model中的字段名变成字典、列表"></a>8、提取Django中model中的字段名变成字典、列表</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import re</span><br><span class="line">t = &quot;&quot;&quot;</span><br><span class="line">    goods_id = models.IntegerField(verbose_name=&#x27;商品编号&#x27;)</span><br><span class="line">    label_code = models.CharField(max_length=20, verbose_name=&#x27;商品标签&#x27;)</span><br><span class="line">&quot;&quot;&quot;</span><br><span class="line"># 字典</span><br><span class="line">print(&#123;k:None for k in re.findall(&#x27;([a-z_A-Z]+)\s=\s&#x27;,t)&#125;)</span><br><span class="line"># 列表</span><br><span class="line"># print([k for k in re.findall(&#x27;([a-z_A-Z]+)\s=\s&#x27;,t)])</span><br><span class="line"></span><br><span class="line">输出</span><br><span class="line">&#123;&#x27;goods_id&#x27;: None, &#x27;lable_code&#x27;: None&#125;</span><br></pre></td></tr></table></figure>
<h2 id="9、数据库中给表创建数据"><a href="#9、数据库中给表创建数据" class="headerlink" title="9、数据库中给表创建数据"></a>9、数据库中给表创建数据</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">import pymysql</span><br><span class="line"></span><br><span class="line">def createData(dataDict,tableName):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    给数据表创建数据</span><br><span class="line">    :param dataDict: 字典</span><br><span class="line">    :param tableName: 表名</span><br><span class="line">    :return: </span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    #连接数据库</span><br><span class="line">    conn = pymysql.connect(</span><br><span class="line">        host=&#x27;192.168.0.188&#x27;, #数据库所在地址URL</span><br><span class="line">        user=&#x27;root&#x27;, #用户名</span><br><span class="line">        password=&#x27;123456&#x27;, #密码</span><br><span class="line">        database=&#x27;名称&#x27;, #数据库名称</span><br><span class="line">        port=3306,  #端口号</span><br><span class="line">        charset=&#x27;utf8&#x27;</span><br><span class="line">    )</span><br><span class="line">    #拿到查询游标</span><br><span class="line">    cursor = conn.cursor()</span><br><span class="line">    clos,value = zip(*dataDict.items())</span><br><span class="line">    sql = &quot;INSERT INTO `%s`(%s) VALUES (%s)&quot; % (tableName,</span><br><span class="line">                                                &#x27;,&#x27;.join(clos),</span><br><span class="line">                                                &#x27;,&#x27;.join([&#x27;%s&#x27;] * len(value))</span><br><span class="line">                                                )</span><br><span class="line">    print(sql)</span><br><span class="line">    cursor.execute(sql, value)</span><br><span class="line">    conn.commit()</span><br><span class="line">    cursor.close()</span><br><span class="line">    conn.close()</span><br><span class="line">    print(&#x27;Done&#x27;)</span><br></pre></td></tr></table></figure>
<h2 id="10-捕捉异常"><a href="#10-捕捉异常" class="headerlink" title="10.捕捉异常"></a>10.捕捉异常</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">try:</span><br><span class="line">    pass</span><br><span class="line">except 异常类型 as e:</span><br><span class="line">    pass</span><br><span class="line">finally:</span><br><span class="line">    pass</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">异常类型</span><br><span class="line">Exception  全部异常</span><br><span class="line">AttributeError 试图访问一个对象没有的属性，比如foo.x，但是foo没有属性x</span><br><span class="line">IOError 输入/输出异常；基本上是无法打开文件</span><br><span class="line">ImportError 无法引入模块或包；基本上是路径问题或名称错误</span><br><span class="line">IndentationError 语法错误（的子类） ；代码没有正确对齐</span><br><span class="line">IndexError 下标索引超出序列边界，比如当x只有三个元素，却试图访问x[5]</span><br><span class="line">KeyError 试图访问字典里不存在的键</span><br><span class="line">KeyboardInterrupt Ctrl+C被按下</span><br><span class="line">NameError 使用一个还未被赋予对象的变量</span><br><span class="line">SyntaxError Python代码非法，代码不能编译(个人认为这是语法错误，写错了）</span><br><span class="line">TypeError 传入对象类型与要求的不符合</span><br><span class="line">UnboundLocalError 试图访问一个还未被设置的局部变量，基本上是由于另有一个同名的全局变量，导致你以为正在访问它</span><br><span class="line">ValueError 传入一个调用者不期望的值，即使值的类型是正确的</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="11、获取当前时间"><a href="#11、获取当前时间" class="headerlink" title="11、获取当前时间"></a>11、获取当前时间</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import datetime</span><br><span class="line"></span><br><span class="line">current_time = str(datetime.datetime.now())[:19]</span><br><span class="line">print(current_time)</span><br><span class="line"></span><br><span class="line">输出格式如：2018-10-20 10:01:43</span><br><span class="line">local_time = time.strftime(&#x27;%Y%m%d%H%M%S&#x27;, time.localtime(time.time()))</span><br><span class="line">print(local_time)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="12、订单编号"><a href="#12、订单编号" class="headerlink" title="12、订单编号"></a>12、订单编号</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">from random import Random</span><br><span class="line">import time</span><br><span class="line"></span><br><span class="line">def random_str(randomlength=8):</span><br><span class="line">    str = &#x27;&#x27;</span><br><span class="line">    chars = &#x27;AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789&#x27;</span><br><span class="line">    length = len(chars) - 1</span><br><span class="line">    random = Random()</span><br><span class="line">    for i in range(randomlength):</span><br><span class="line">        str+=chars[random.randint(0, length)]</span><br><span class="line">    return str</span><br><span class="line"></span><br><span class="line">def order_num():</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    生成付款订单号</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    local_time = time.strftime(&#x27;%Y%m%d%H%M%S&#x27;, time.localtime(time.time()))</span><br><span class="line">    result = local_time + random_str(5)</span><br><span class="line">    return result</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">print(order_num())</span><br></pre></td></tr></table></figure>
<h2 id="13、mysql自动填写当前时间"><a href="#13、mysql自动填写当前时间" class="headerlink" title="13、mysql自动填写当前时间"></a>13、mysql自动填写当前时间</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CURRENT_TIMESTAMP</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20190312172747658.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">为表添加索引</span><br><span class="line"></span><br><span class="line">ALTER table tableName ADD INDEX indexName(columnName)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="14、drf动态过滤查询"><a href="#14、drf动态过滤查询" class="headerlink" title="14、drf动态过滤查询"></a>14、drf动态过滤查询</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"># page.py</span><br><span class="line"></span><br><span class="line">from rest_framework.pagination import PageNumberPagination</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class UserPagination(PageNumberPagination):</span><br><span class="line">    &quot;&quot;&quot;用户分页器&quot;&quot;&quot;</span><br><span class="line">    page_size = 10  # 默认的页面数据数量</span><br><span class="line">    page_query_param = &#x27;page&#x27;  # 定制取数据页码key </span><br><span class="line">    page_size_query_param = &#x27;page_size&#x27;  # 默认取数据页码key </span><br><span class="line">    max_page_size = 15   # 数据每页取值的最大上限</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># serializers.py</span><br><span class="line"></span><br><span class="line">from rest_framework import serializers</span><br><span class="line"></span><br><span class="line">from user.models import UserInfo</span><br><span class="line"></span><br><span class="line">class UserSerializers(serializers.ModelSerializer):</span><br><span class="line">    &quot;&quot;&quot;用户收货地址&quot;&quot;&quot;</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        model = UserInfo</span><br><span class="line">	    # 所有字段</span><br><span class="line">        #fields = &#x27;__all__&#x27;</span><br><span class="line">        fields = [&#x27;name&#x27;, &#x27;code&#x27;, &#x27;title&#x27;,  &#x27;province&#x27;, &#x27;city&#x27;,</span><br><span class="line">                    &#x27;quxian&#x27;, &#x27;address&#x27;, &#x27;code__gte&#x27;, &#x27;code__lte&#x27;]</span><br><span class="line">        # 显示外键</span><br><span class="line">        depth = 2</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># views.py</span><br><span class="line">class MachineViews(APIView):</span><br><span class="line">    def get(self, request, *args, **kwargs):</span><br><span class="line">        # 从前端获取出来的过滤参数，解析成字典传进filter()函数中</span><br><span class="line">        # 动态过滤，</span><br><span class="line">        kwargs = &#123;&#125;</span><br><span class="line">        # 表中的字段名</span><br><span class="line">        columns = [&#x27;name&#x27;, &#x27;code&#x27;, &#x27;title&#x27;,  &#x27;province&#x27;, &#x27;city&#x27;,</span><br><span class="line">                    &#x27;quxian&#x27;, &#x27;address&#x27;, &#x27;code__gte&#x27;, &#x27;code__lte&#x27;]</span><br><span class="line">        for k, v in request.query_params.items():</span><br><span class="line">            if k not in columns:</span><br><span class="line">                return Response(&#x27;参数不对&#x27;, status=status.HTTP_400_BAD_REQUEST)</span><br><span class="line">			if v:</span><br><span class="line">            	kwargs[k] = v</span><br><span class="line"></span><br><span class="line">        users = UserInfo.objects.filter(**kwargs)</span><br><span class="line">        page = UserPagination()</span><br><span class="line">        page_goods_list = page.paginate_queryset(users, self.request, self)</span><br><span class="line">        ser = UserSerializers(page_goods_list, many=True)</span><br><span class="line">        return page.get_paginated_response(ser.data)</span><br><span class="line">        </span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="15、linux后台运行python程序"><a href="#15、linux后台运行python程序" class="headerlink" title="15、linux后台运行python程序"></a>15、linux后台运行python程序</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">nohup /home/project_venv/user/bin/python3 -u /home/user/user_server.py &gt;&gt; /home/user/user.log 2&gt;&amp;1 &amp;   </span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="16、追加外键"><a href="#16、追加外键" class="headerlink" title="16、追加外键"></a>16、追加外键</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ALTER TABLE tb_commentPhoto ADD CONSTRAINT FK_comment_phone</span><br><span class="line">FOREIGN KEY tb_goodsComment(id) REFERENCES tb_commentPhoto(comment_id);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="17、写-读CSV文件，查看是否存在，若存在就从csv中删除"><a href="#17、写-读CSV文件，查看是否存在，若存在就从csv中删除" class="headerlink" title="17、写/读CSV文件，查看是否存在，若存在就从csv中删除"></a>17、写/读CSV文件，查看是否存在，若存在就从csv中删除</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">import csv</span><br><span class="line">import random</span><br><span class="line">import string</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def create_invite_code(random_code_pool=None, length=6, num=10, is_append=False):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    创建随机邀请码,并写入txt文件</span><br><span class="line">    :param: random_code_pool 随机邀请码</span><br><span class="line">    :param: length 邀请码长度</span><br><span class="line">    :param: num 邀请码个数</span><br><span class="line">    :param: is_append True追加,False 覆盖</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    if not random_code_pool:</span><br><span class="line">        code_pool = string.ascii_uppercase + string.digits</span><br><span class="line">        random_code_pool = []</span><br><span class="line">        for i in range(num):</span><br><span class="line">            s = &#x27;&#x27;</span><br><span class="line">            for _ in range(length):</span><br><span class="line">                s += random.choice(code_pool)</span><br><span class="line">            if s and s not in random_code_pool:</span><br><span class="line">                random_code_pool.append(s)</span><br><span class="line"></span><br><span class="line">    # 写入方法。是追加还是覆盖</span><br><span class="line">    write_method = &#x27;a+&#x27; if is_append else &#x27;w&#x27;</span><br><span class="line">    # 写入文件</span><br><span class="line">    with open(&#x27;./invite_code.csv&#x27;, write_method, newline=&#x27;&#x27;) as f:</span><br><span class="line">        writer = csv.writer(f)</span><br><span class="line">        for rowData in random_code_pool:</span><br><span class="line">            # 按行写入</span><br><span class="line">            writer.writerow((rowData,))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def check_invite_code(code):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    查看邀请码是否存在txt文件中，</span><br><span class="line">    若存在就返回True，并在txt文件中删除</span><br><span class="line">    若不存在就返回False</span><br><span class="line">    :param code:</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    code_pool = []</span><br><span class="line">    with open(&#x27;./invite_code.csv&#x27;, &#x27;r&#x27;, encoding=&#x27;utf-8&#x27;,errors=&#x27;ignore&#x27;) as f:</span><br><span class="line">        allFileInfo = csv.reader(f)</span><br><span class="line">        for row in allFileInfo:</span><br><span class="line">            code_pool.append(row[0])</span><br><span class="line"></span><br><span class="line">    if code in code_pool:</span><br><span class="line">        # 删除查询的code</span><br><span class="line">        code_pool.pop(code_pool.index(code))</span><br><span class="line"></span><br><span class="line">        # 重新写入文件</span><br><span class="line">        create_invite_code(code_pool,is_append=False)</span><br><span class="line">        return True</span><br><span class="line">    return False</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == &#x27;__main__&#x27;:</span><br><span class="line">    # create_invite_code(length=9,num=100)</span><br><span class="line">    print(check_invite_code(&#x27;WJ4PSTJG2&#x27;))</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="18、django中从request获取访问路径"><a href="#18、django中从request获取访问路径" class="headerlink" title="18、django中从request获取访问路径"></a>18、django中从request获取访问路径</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">print(&#x27;获取相对路径&#x27;, request.get_full_path())</span><br><span class="line">print(&#x27;获取绝对路径&#x27;, request.build_absolute_uri())</span><br><span class="line">print(request.build_absolute_uri(&#x27;?&#x27;))</span><br><span class="line">print(request.build_absolute_uri(&#x27;/&#x27;)[:-1].strip(&quot;/&quot;))</span><br><span class="line">print(request.build_absolute_uri(&#x27;/&#x27;).strip(&quot;/&quot;))</span><br><span class="line">print(request.build_absolute_uri(&#x27;/&#x27;))</span><br><span class="line">print(&#x27;----------&#x27;)</span><br><span class="line">print(request.META[&#x27;HTTP_HOST&#x27;])</span><br><span class="line">print(request.META[&#x27;PATH_INFO&#x27;])</span><br><span class="line">print(request.META[&#x27;QUERY_STRING&#x27;])</span><br><span class="line">iphost = request.META.get(&#x27;REMOTE_ADDR&#x27;, &#x27;&#x27;)  # 获取访问来源IP</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 输出如：</span><br><span class="line">获取相对路径 /QRcode/?d=1</span><br><span class="line">获取绝对路径 http://127.0.0.1:8000/QRcode/?d=1</span><br><span class="line">http://127.0.0.1:8000/QRcode/</span><br><span class="line">http://127.0.0.1:8000</span><br><span class="line">http://127.0.0.1:8000</span><br><span class="line">http://127.0.0.1:8000/</span><br><span class="line">----------</span><br><span class="line">127.0.0.1:8000</span><br><span class="line">/QRcode/</span><br><span class="line">d=1</span><br></pre></td></tr></table></figure>
<h2 id="19、Django收集静态文件"><a href="#19、Django收集静态文件" class="headerlink" title="19、Django收集静态文件"></a>19、Django收集静态文件</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">先在项目根目录下创建一个static文件夹</span><br><span class="line">然后在settings.py中设置</span><br><span class="line">STATIC_ROOT = os.path.join(BASE_DIR, &#x27;static&#x27;)</span><br><span class="line">最后执行下面的命令：</span><br><span class="line">python manage.py collectstatic</span><br></pre></td></tr></table></figure>
<h2 id="20、xadmin插件"><a href="#20、xadmin插件" class="headerlink" title="20、xadmin插件"></a>20、xadmin插件</h2><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/lanqie/p/8340215.html">https://www.cnblogs.com/lanqie/p/8340215.html</a></p>
<h2 id="21、uwsgi自动重启"><a href="#21、uwsgi自动重启" class="headerlink" title="21、uwsgi自动重启"></a>21、uwsgi自动重启</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">[uwsgi]</span><br><span class="line"># 使用nginx连接时 使用</span><br><span class="line">socket=0.0.0.0:2019</span><br><span class="line"># 直接作为web服务器使用</span><br><span class="line">#http=0.0.0.1:8000</span><br><span class="line"># 配置工程目录</span><br><span class="line">chdir=/home/user</span><br><span class="line"># 配置项目的wsgi目录。相对于工程目录</span><br><span class="line">wsgi-file=user/wsgi.py</span><br><span class="line">virtualenv=/home/project_venv/user</span><br><span class="line">#配置进程，线程信息</span><br><span class="line">processes=1</span><br><span class="line">threads=1</span><br><span class="line">enable-threads=True</span><br><span class="line">master=True</span><br><span class="line">pidfile=uwsgi.pid</span><br><span class="line">daemonize=uwsgi.log</span><br><span class="line">#启动uwsgi的用户名和用户组</span><br><span class="line">uid=root</span><br><span class="line">gid=root</span><br><span class="line">#uwsgi自动重启</span><br><span class="line">py-autoreload=1</span><br></pre></td></tr></table></figure>
<h2 id="22、谷歌浏览器快捷键"><a href="#22、谷歌浏览器快捷键" class="headerlink" title="22、谷歌浏览器快捷键"></a>22、谷歌浏览器快捷键</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">浏览器缓存</span><br><span class="line"></span><br><span class="line">Ctrl+Shift+Del  清除Google浏览器缓存的快捷键</span><br><span class="line"></span><br><span class="line">Ctrl+Shift+R  重新加载当前网页而不使用缓存内容</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="23、git克隆分支"><a href="#23、git克隆分支" class="headerlink" title="23、git克隆分支"></a>23、git克隆分支</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone -b dev 地址 </span><br></pre></td></tr></table></figure>
<h2 id="24、mysql更新语句、新增列、删除列"><a href="#24、mysql更新语句、新增列、删除列" class="headerlink" title="24、mysql更新语句、新增列、删除列"></a>24、mysql更新语句、新增列、删除列</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">update user set name=&#x27;张三&#x27; where id=111</span><br><span class="line"># 删除</span><br><span class="line">DELETE FROM table_name [WHERE Clause]</span><br><span class="line"></span><br><span class="line"># 增加字段</span><br><span class="line">alter table 表名 add column 列名 类型;</span><br><span class="line"># 删除字段</span><br><span class="line">alter table 表名 dropcolumn 列名 ;</span><br></pre></td></tr></table></figure>
<h2 id="25、删除指定格式的文件"><a href="#25、删除指定格式的文件" class="headerlink" title="25、删除指定格式的文件"></a>25、删除指定格式的文件</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">import os</span><br><span class="line">import re</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def remove_specified_format_file(file_dir, format_name):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    删除指定格式的文件</span><br><span class="line">    :param file_dir: 文件根目录</span><br><span class="line">    :param format_name: 格式</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    for root, dirs, files in os.walk(file_dir):</span><br><span class="line">        # print(root) #当前目录路径</span><br><span class="line">        # print(dirs) #当前路径下所有子目录</span><br><span class="line">        # print(files) #当前路径下所有非目录子文件</span><br><span class="line">        for file in files:</span><br><span class="line">            if re.match(format_name, file):</span><br><span class="line">                print(os.path.join(root, file))</span><br><span class="line">                os.remove(os.path.join(root, file))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">remove_specified_format_file(r&#x27;D:\学习\LDC\java&#x27;, r&#x27;\._*&#x27;)</span><br></pre></td></tr></table></figure>
<h2 id="26、计算文件总数"><a href="#26、计算文件总数" class="headerlink" title="26、计算文件总数"></a>26、计算文件总数</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">import os</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def file_count(file_dir):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    </span><br><span class="line">    :param file_dir: 文件根目录</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    count = 0</span><br><span class="line">    for root, dirs, files in os.walk(file_dir):</span><br><span class="line">        # print(root) #当前目录路径</span><br><span class="line">        # print(dirs) #当前路径下所有子目录</span><br><span class="line">        # print(files) #当前路径下所有非目录子文件</span><br><span class="line">        count += len(files)</span><br><span class="line">    return count</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">print(file_count(r&#x27;D:\学习\LDC\java\Java学习\newEstore\estore\js&#x27;))</span><br></pre></td></tr></table></figure>
<h2 id="27、计算文件夹大小"><a href="#27、计算文件夹大小" class="headerlink" title="27、计算文件夹大小"></a>27、计算文件夹大小</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import os</span><br><span class="line"></span><br><span class="line">def file_size(file_dir):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    删除指定格式的文件</span><br><span class="line">    :param file_dir: 文件根目录</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    size = 0</span><br><span class="line">    for root, dirs, files in os.walk(file_dir):</span><br><span class="line">        # print(root) #当前目录路径</span><br><span class="line">        # print(dirs) #当前路径下所有子目录</span><br><span class="line">        # print(files) #当前路径下所有非目录子文件</span><br><span class="line">        for file in files:</span><br><span class="line">            size += os.path.getsize(os.path.join(root, file))</span><br><span class="line">	# M为单位</span><br><span class="line">    return size / 1024 / 1024</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">file_name = r&#x27;D:\学习&#x27;</span><br><span class="line">print(file_size(file_name))</span><br></pre></td></tr></table></figure>
<h2 id="28、Django实现jsonp跨域"><a href="#28、Django实现jsonp跨域" class="headerlink" title="28、Django实现jsonp跨域"></a>28、Django实现jsonp跨域</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"># html</span><br><span class="line">$.ajax(&#123;</span><br><span class="line">                    url: &#x27;请求路由&#x27;,</span><br><span class="line">                    type: &#x27;GET&#x27;,</span><br><span class="line">                    dataType: &#x27;JSONP&#x27;,</span><br><span class="line">                    data:&#123;</span><br><span class="line">                      code: &#x27;yes&#x27;,</span><br><span class="line">                    &#125;,</span><br><span class="line">                    jsonp: &#x27;callback&#x27;,</span><br><span class="line">                    success: function(res) &#123;</span><br><span class="line">                       var selectData = $.parseJSON(res);</span><br><span class="line">                       alert(selectData);</span><br><span class="line">                    &#125;,</span><br><span class="line">                    error: function(err) &#123;</span><br><span class="line"></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line"></span><br><span class="line"># views.py</span><br><span class="line">def get(self, request, *args, **kwargs):</span><br><span class="line">        code = request.GET.get(&#x27;code&#x27;, &#x27;&#x27;)</span><br><span class="line">        # 跨域请求</span><br><span class="line">        callback = request.GET.get(&#x27;callback&#x27;, &#x27;&#x27;)</span><br><span class="line">        return HttpResponse(&quot;%s(&#x27;%s&#x27;)&quot; % (callback, json.dumps(&#123;&#x27;code&#x27;: code&#125;)), status=status.HTTP_200_OK)</span><br><span class="line">                    </span><br><span class="line">cors解决跨域</span><br><span class="line"></span><br><span class="line">https://www.cnblogs.com/wxiaoyu/p/9578848.html</span><br></pre></td></tr></table></figure>
<h2 id="29、微信获取用户信息"><a href="#29、微信获取用户信息" class="headerlink" title="29、微信获取用户信息"></a>29、微信获取用户信息</h2><p>参考：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_39735923/article/details/79202563">https://blog.csdn.net/weixin_39735923/article/details/79202563</a></p>
<h2 id="30、uwsgi初始配置问题"><a href="#30、uwsgi初始配置问题" class="headerlink" title="30、uwsgi初始配置问题"></a>30、uwsgi初始配置问题</h2><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_39735923/article/details/79202563">https://blog.csdn.net/weixin_39735923/article/details/79202563</a></p>
<h2 id="31、django中drf序列化"><a href="#31、django中drf序列化" class="headerlink" title="31、django中drf序列化"></a>31、django中drf序列化</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"># 序列化器</span><br><span class="line">class MsgSerializers(serializers.ModelSerializer):</span><br><span class="line">    addtime = serializers.DateTimeField(read_only=True, format=&quot;%Y-%m-%d %H:%M:%S&quot;)</span><br><span class="line">    hasread = serializers.CharField(source=&#x27;get_hasread_display&#x27;)</span><br><span class="line">    msgtype = serializers.CharField(source=&#x27;get_msgtype_display&#x27;)</span><br><span class="line">    class Meta:</span><br><span class="line">        model = MallMsg</span><br><span class="line">        # 可以混合使用</span><br><span class="line">        fields = &#x27;__all__&#x27;  # &#x27;__all__&#x27; 所有字段</span><br><span class="line">        # 数据库层级控制（序列化链表操作）</span><br><span class="line">        # depth = 1  # 外键层级</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#分页器</span><br><span class="line">from rest_framework.pagination import PageNumberPagination</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class MyLimitOffsetPagination(PageNumberPagination):</span><br><span class="line"></span><br><span class="line">    page_size = 3  # 默认的页面数据数量</span><br><span class="line">    page_query_param = &#x27;page&#x27;  # 定制取数据页码key ？</span><br><span class="line">    page_size_query_param = &#x27;page_size&#x27;  # 默认取数据页码key &amp;</span><br><span class="line">    max_page_size = 15  # 数据每页取值的最大上限</span><br></pre></td></tr></table></figure>
<h2 id="32、第三方库"><a href="#32、第三方库" class="headerlink" title="32、第三方库"></a>32、第三方库</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">安装openssl</span><br><span class="line"> pip3 install pyOpenSSL</span><br></pre></td></tr></table></figure>
<h2 id="33、requests请求https携带CA证书"><a href="#33、requests请求https携带CA证书" class="headerlink" title="33、requests请求https携带CA证书"></a>33、requests请求https携带CA证书</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">import OpenSSL</span><br><span class="line">import requests</span><br><span class="line">import urllib3.contrib.pyopenssl</span><br><span class="line"></span><br><span class="line">urllib3.contrib.pyopenssl.inject_into_urllib3()</span><br><span class="line"></span><br><span class="line">def p12_to_pem(certname, pwd):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    从.p12文件中提取pem</span><br><span class="line">    :param certname:</span><br><span class="line">    :param pwd:</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    pem_name = certname + &quot;.pem&quot;</span><br><span class="line">    f_pem = open(pem_name, &#x27;wb&#x27;)</span><br><span class="line">    p12file = certname + &quot;.p12&quot;</span><br><span class="line">    p12 = OpenSSL.crypto.load_pkcs12(open(p12file, &#x27;rb&#x27;).read(), pwd)</span><br><span class="line">    f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey()))</span><br><span class="line">    f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate()))</span><br><span class="line">    ca = p12.get_ca_certificates()</span><br><span class="line">    if ca is not None:</span><br><span class="line">        for cert in ca:</span><br><span class="line">            f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert))</span><br><span class="line">    f_pem.close()</span><br><span class="line">    return pem_name</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def post_cert_request(url, data,header, certname, pwd):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    使用证书发起https请求</span><br><span class="line">    :param url:</span><br><span class="line">    :param data:</span><br><span class="line">    :param certname:</span><br><span class="line">    :param pwd:</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    if (certname != &quot;&quot;):</span><br><span class="line">        cert = p12_to_pem(certname, pwd)</span><br><span class="line">    else:</span><br><span class="line">        cert = None</span><br><span class="line">    r = requests.post(url, header=header, data=data, cert=cert)</span><br><span class="line">    return r</span><br></pre></td></tr></table></figure>
<h2 id="34、django创建缓存命令"><a href="#34、django创建缓存命令" class="headerlink" title="34、django创建缓存命令"></a>34、django创建缓存命令</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py createcachetable 缓存表名</span><br></pre></td></tr></table></figure>
<h2 id="35、Django-更改超级用户密码"><a href="#35、Django-更改超级用户密码" class="headerlink" title="35、Django 更改超级用户密码"></a>35、Django 更改超级用户密码</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">在工程文件目录下敲入：</span><br><span class="line">python manage.py shell</span><br><span class="line"></span><br><span class="line">再在python交互界面输入：</span><br><span class="line">from django.contrib.auth.models import User</span><br><span class="line">user = User.objects.get(username = &#x27;用户名&#x27;)</span><br><span class="line">user.set_password(&#x27;密码&#x27;)</span><br><span class="line">user.save()</span><br></pre></td></tr></table></figure>
<h2 id="36、restframe使用缓存"><a href="#36、restframe使用缓存" class="headerlink" title="36、restframe使用缓存"></a>36、restframe使用缓存</h2><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/Odyssues_lee/article/details/80872586">https://blog.csdn.net/Odyssues_lee/article/details/80872586</a></p>
<h2 id="37、数据库"><a href="#37、数据库" class="headerlink" title="37、数据库"></a>37、数据库</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select * from user  where ISNULL(code)</span><br><span class="line">update user set code=&#x27;111&#x27;,info=&#x27;微信&#x27; where ISNULL(code)</span><br></pre></td></tr></table></figure>
<h2 id="38、linux常用命令"><a href="#38、linux常用命令" class="headerlink" title="38、linux常用命令"></a>38、linux常用命令</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">tail -f 日志名  实时监控日志</span><br><span class="line">tail -f 80_v10.log</span><br><span class="line"></span><br><span class="line">netstat -na|grep 80  查看端口tcp连接数</span><br><span class="line">netstat -na|grep 80 | wc -l 计算端口tcp连接数</span><br><span class="line"></span><br><span class="line">ps -ef|grep python  查看有多少python程序在运行</span><br><span class="line"></span><br><span class="line">gunzip 2015.csv.gz   # 解压</span><br><span class="line">unzip 19.zip # 解压zip</span><br><span class="line">wc -l 2015.csv   # 查看行数</span><br><span class="line">apt install lrzsz  # 安装</span><br><span class="line">sz 文件名  # 下载文件</span><br><span class="line"></span><br><span class="line">查找文件</span><br><span class="line">find / -name 文件名</span><br><span class="line"></span><br><span class="line">匹配执行过的以find为开头的命令</span><br><span class="line">history | grep find</span><br></pre></td></tr></table></figure>
<h2 id="39、xadmin禁止增加、删除"><a href="#39、xadmin禁止增加、删除" class="headerlink" title="39、xadmin禁止增加、删除"></a>39、xadmin禁止增加、删除</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"># models.py</span><br><span class="line"># 用户管理</span><br><span class="line">class UserManage(models.Model):</span><br><span class="line">    name = models.CharField(max_length=20, verbose_name=&#x27;用户名&#x27;)</span><br><span class="line">    phone = models.CharField(max_length=11, unique=True, verbose_name=&#x27;手机号&#x27;)</span><br><span class="line">    code = models.CharField(max_length=11, unique=True, verbose_name=&#x27;编号&#x27;)</span><br><span class="line">    </span><br><span class="line">    user = models.ForeignKey(User, on_delete=models.CASCADE, editable=False, null=True, verbose_name=&#x27;管理员&#x27;)</span><br><span class="line"></span><br><span class="line"># adminx.py</span><br><span class="line"># 用户列表</span><br><span class="line">class UserAdmin(object):</span><br><span class="line">    list_display = [ &#x27;code&#x27;, &#x27;phone&#x27;, &#x27;name&#x27;,]</span><br><span class="line">    search_fields = [&#x27;code&#x27;, &#x27;phone&#x27;]</span><br><span class="line">    list_filter = [&#x27;code&#x27;, &#x27;phone&#x27;]</span><br><span class="line">    list_editable = [&#x27;name&#x27;]  # 数据即时编辑</span><br><span class="line">    readonly_fields = [&#x27;code&#x27;, &#x27;phone&#x27;, &#x27;name&#x27;]  # 只读字段，不能编辑</span><br><span class="line">    model_icon = &#x27;fa fa-square&#x27;</span><br><span class="line">    model = UserInfo</span><br><span class="line">    </span><br><span class="line">    def has_delete_permission(self, *args, **kwargs):</span><br><span class="line">    	# 禁止删除</span><br><span class="line">        if args:</span><br><span class="line">            return True</span><br><span class="line">        return False</span><br><span class="line">    </span><br><span class="line">	def has_add_permission(self,*args,**kwargs):</span><br><span class="line">		# 禁止增加</span><br><span class="line">        return False</span><br><span class="line">        </span><br><span class="line">    def save_models(self):</span><br><span class="line">    	# 用户级别设置</span><br><span class="line">        self.new_obj.user = self.request.user</span><br><span class="line">        flag = self.org_obj is None and &#x27;create&#x27; or &#x27;change&#x27;</span><br><span class="line">        if flag == &#x27;create&#x27;:</span><br><span class="line">        	# 对密码字段进行加密</span><br><span class="line">            self.new_obj.password = encrypt_oracle(self.new_obj.password)</span><br><span class="line">        elif flag == &#x27;change&#x27;:</span><br><span class="line">            if &#x27;password&#x27; in self.change_message():</span><br><span class="line">                self.new_obj.password = encrypt_oracle(self.new_obj.password)</span><br><span class="line">        else:</span><br><span class="line">        	pass</span><br><span class="line">        super().save_models()</span><br><span class="line"></span><br><span class="line">xadmin.site.register(UserInfo, UserAdmin)</span><br></pre></td></tr></table></figure>
<h2 id="40、时间格式字符串相减"><a href="#40、时间格式字符串相减" class="headerlink" title="40、时间格式字符串相减"></a>40、时间格式字符串相减</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import datetime</span><br><span class="line">import time</span><br><span class="line"></span><br><span class="line">start = str(datetime.datetime.now())[:19]</span><br><span class="line">time.sleep(60)</span><br><span class="line">end = str(datetime.datetime.now())[:19]</span><br><span class="line">print(start,end)</span><br><span class="line">link_start = datetime.datetime.strptime(start, &#x27;%Y-%m-%d %H:%M:%S&#x27;)</span><br><span class="line">link_end = datetime.datetime.strptime(end, &#x27;%Y-%m-%d %H:%M:%S&#x27;)</span><br><span class="line">link_min = round((link_end - link_start).seconds / 60, 2)</span><br><span class="line">print(link_min,&#x27;分钟&#x27;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="41、显示循环进度条"><a href="#41、显示循环进度条" class="headerlink" title="41、显示循环进度条"></a>41、显示循环进度条</h2><p>参考：<a target="_blank" rel="noopener" href="https://blog.csdn.net/zejianli/article/details/77915751">https://blog.csdn.net/zejianli/article/details/77915751</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">from tqdm import tqdm,trange</span><br><span class="line">from time import sleep</span><br><span class="line">text = &quot;&quot;</span><br><span class="line">for char in tqdm([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]):</span><br><span class="line">    text = text + char</span><br><span class="line">    sleep(1)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"># 方式二</span><br><span class="line">import time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def process_bar(percent, index, total,start_time, start_str=&#x27;&#x27;, end_str=&#x27;&#x27;, total_length=100):</span><br><span class="line">    # 进度条</span><br><span class="line">    percent_length = int(percent)</span><br><span class="line">    bar = &#x27;\r&#x27; + start_str + (&#x27;\033[1;31;41m \033[0m&#x27; * percent_length + &#x27;\033[1;37;47m \033[0m&#x27; * (</span><br><span class="line">                total_length - percent_length)) + f&#x27;  &#123;round(index / total * 100, 2)&#125;%  &#x27; + f&#x27; &#123;index&#125;|&#123;end_str&#125;&#x27;+ f&#x27;   |已进行时间: &#123;round(time.time() - start_time, 2)&#125;秒&#x27;</span><br><span class="line"></span><br><span class="line">    print(bar, end=&#x27;&#x27;, flush=True)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == &#x27;__main__&#x27;:</span><br><span class="line">    data_set = [i for i in range(23)]</span><br><span class="line">    i = 0</span><br><span class="line">    start_time = time.time()</span><br><span class="line">    total = len(data_set)</span><br><span class="line">    end_str = &#x27;&#123;&#125;&#x27;.format(total)</span><br><span class="line">    for data in data_set:</span><br><span class="line">        time.sleep(1)</span><br><span class="line">        i += 1</span><br><span class="line">        process_bar(i * 100 / total, i, total, start_time, start_str=&#x27;&#x27;, end_str=end_str, total_length=100)</span><br><span class="line"></span><br><span class="line"># 方式三</span><br><span class="line">import sys</span><br><span class="line">import time</span><br><span class="line"></span><br><span class="line">d = [i for i in range(100)]</span><br><span class="line">for i in range(len(d)):</span><br><span class="line">    time.sleep(1)</span><br><span class="line">    sys.stdout.write(&#x27;\r&gt;&gt; Downloading  %.2f%%&#x27; % (float(i) / float(len(d)) * 100.0))</span><br><span class="line">sys.stdout.flush()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="42、把列表中的字典转成csv文件"><a href="#42、把列表中的字典转成csv文件" class="headerlink" title="42、把列表中的字典转成csv文件"></a>42、把列表中的字典转成csv文件</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">import pandas as pd</span><br><span class="line">lists = [&#123;&#x27;a&#x27;:1,&#x27;b&#x27;:2&#125;,&#123;&#x27;a&#x27;:2,&#x27;b&#x27;:3&#125;]</span><br><span class="line">df = pd.DataFrame(lists)</span><br><span class="line">print(df)</span><br><span class="line">df.to_csv(&#x27;result2.csv&#x27;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="43、windows添加右键新建MarkDown文件"><a href="#43、windows添加右键新建MarkDown文件" class="headerlink" title="43、windows添加右键新建MarkDown文件"></a>43、windows添加右键新建MarkDown文件</h2><p>在网上下载Typora软件安装后<br>1、在桌面上新建一个txt文件，输入以下内容：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Windows Registry Editor Version 5.00</span><br><span class="line">[HKEY_CLASSES_ROOT\.md\ShellNew]</span><br><span class="line">&quot;NullFile&quot;=&quot;&quot;</span><br><span class="line">&quot;FileName&quot;=&quot;template.md&quot;</span><br></pre></td></tr></table></figure>
<p>2、另存为，改后缀为.reg，保存类型为.txt，编码为Unicode</p>
<p><img src="https://img-blog.csdnimg.cn/20200526232015169.png" alt="在这里插入图片描述"></p>
<p>3、双击运行，确定，重启电脑，此时在桌面右键就有了新建md文件</p>
<h2 id="44、redis设置值定时过期"><a href="#44、redis设置值定时过期" class="headerlink" title="44、redis设置值定时过期"></a>44、redis设置值定时过期</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">import datetime</span><br><span class="line">import redis</span><br><span class="line"></span><br><span class="line">redis_client = redis.Redis(</span><br><span class="line">    host=&#x27;127.0.0.1&#x27;,</span><br><span class="line">    port=6379,</span><br><span class="line">    db=0,</span><br><span class="line">    password=&#x27;123456&#x27;</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">def redis_set():</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    redis设置值定时过期</span><br><span class="line">    :return:</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    global redis_client</span><br><span class="line"></span><br><span class="line">    redis_client.set(&#x27;name&#x27;,&#x27;ldc&#x27;)</span><br><span class="line">    now = datetime.datetime.now()</span><br><span class="line">    # 设置‘name’50秒过期</span><br><span class="line">    expire_time = now + datetime.timedelta(hours=0, minutes=0, seconds=50)</span><br><span class="line">    redis_client.expireat(&#x27;name&#x27;, expire_time)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == &#x27;__main__&#x27;:</span><br><span class="line">    redis_set()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="45、linux根据端口杀进程"><a href="#45、linux根据端口杀进程" class="headerlink" title="45、linux根据端口杀进程"></a>45、linux根据端口杀进程</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import os</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def killport(port):</span><br><span class="line">    command = &#x27;&#x27;&#x27;kill -9 $(netstat -nlp | grep :&#x27;&#x27;&#x27; + str(port) + &#x27;&#x27;&#x27; | awk &#x27;&#123;print $7&#125;&#x27; | awk -F&quot;/&quot; &#x27;&#123; print $1 &#125;&#x27;)&#x27;&#x27;&#x27;</span><br><span class="line">    os.system(command)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 开始执行</span><br><span class="line">if __name__ == &#x27;__main__&#x27;:</span><br><span class="line">    port = 4237</span><br><span class="line">    killport(port)</span><br></pre></td></tr></table></figure>
<h2 id="46、监控linux网络流量"><a href="#46、监控linux网络流量" class="headerlink" title="46、监控linux网络流量"></a>46、监控linux网络流量</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">iftop -n -N -i eth0</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">nethogs eth0</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">vim +/字符串 文件</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="47、win10添加右键打开cmd"><a href="#47、win10添加右键打开cmd" class="headerlink" title="47、win10添加右键打开cmd"></a>47、win10添加右键打开cmd</h2><p>通过添加注册表项实现</p>
<p>win + r 输入 regedit</p>
<p>找到注册表位置：HKEY_CLASSES_ROOT\Directory\Background\shell\</p>
<p>右键“shel”l,新建“项”，命名为“以管理员身份打开cmd”，</p>
<p>右键“以管理员身份打开cmd”，新建“DWORD(32位)值”，命名为”ShowBasedOnVelocityId”,值为“639bc8”</p>
<p>右键“以管理员身份打开cmd”，新建“项”，命名为“command”</p>
<p>右键“command”，点击“默认”，点击“修改”，填写数值为 cmd.exe /s /k pushd “%V”</p>
<h2 id="48、xadmin后台无法显示下拉框完整内容"><a href="#48、xadmin后台无法显示下拉框完整内容" class="headerlink" title="48、xadmin后台无法显示下拉框完整内容"></a>48、xadmin后台无法显示下拉框完整内容</h2><p>解决方案 在根目录中找到/static/xadmin/vendor/selectize/selectize.bootstrap3.css<br>在331行后加入 position: static;</p>
<p><img src="https://img-blog.csdnimg.cn/20190704184201572.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h2 id="49、xadmin单点登录"><a href="#49、xadmin单点登录" class="headerlink" title="49、xadmin单点登录"></a>49、xadmin单点登录</h2><p>使用中间件实现。</p>
<p>新建一个utils.py文件，存放以下代码：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">from django.contrib.sessions.models import Session</span><br><span class="line">from django.db.models import Q</span><br><span class="line">from django.utils import timezone</span><br><span class="line">from django.utils.deprecation import MiddlewareMixin</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class XadminMiddleware(MiddlewareMixin):</span><br><span class="line">    def process_request(self, request):</span><br><span class="line">        &quot;&quot;&quot;xadmin后台单点登录&quot;&quot;&quot;</span><br><span class="line">        PATH_INFO = request.META.get(&#x27;PATH_INFO&#x27;, &#x27;&#x27;)</span><br><span class="line">        if PATH_INFO and &#x27;xadmin&#x27; in PATH_INFO:</span><br><span class="line">            request.session.clear_expired()   # 清除过期的key</span><br><span class="line">            session_key = request.session.session_key</span><br><span class="line">            for session in Session.objects.filter(~Q(session_key=session_key), expire_date__gte=timezone.now()):</span><br><span class="line">                data = session.get_decoded()</span><br><span class="line">                if data.get(&#x27;_auth_user_id&#x27;, None) == str(request.user.id):</span><br><span class="line">                    session.delete()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>然后在urls.py中设置：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">urlpatterns = [</span><br><span class="line">...</span><br><span class="line">re_path(&#x27;^xadmin/&#x27;, xadmin.site.urls),</span><br><span class="line">...</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p>然后在settings.py中注册中间件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">   MIDDLEWARE = [</span><br><span class="line">   ...</span><br><span class="line">   </span><br><span class="line">   &#x27;utils.xadminauth.XadminMiddleware&#x27;,</span><br><span class="line">   ...</span><br><span class="line">   ]</span><br><span class="line">SESSION_COOKIE_AGE = 1209600  # 设置过期时间</span><br><span class="line">   SESSION_SAVE_EVERY_REQUEST = Ture  # 每次请求都更新</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>【参考】 <a target="_blank" rel="noopener" href="https://blog.csdn.net/Python_anning">https://blog.csdn.net/Python_anning</a></p>
<h2 id="50、Django-restful-多个models数据表序列化合并返回（一次请求返回多个序列化器数据）"><a href="#50、Django-restful-多个models数据表序列化合并返回（一次请求返回多个序列化器数据）" class="headerlink" title="50、Django restful 多个models数据表序列化合并返回（一次请求返回多个序列化器数据）"></a>50、Django restful 多个models数据表序列化合并返回（一次请求返回多个序列化器数据）</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"># 导入第三方包</span><br><span class="line">pip install django-crispy-forms==1.7.2</span><br><span class="line"></span><br><span class="line"># 在settings.py中添加应用</span><br><span class="line">INSTALLED_APPS = [</span><br><span class="line">...</span><br><span class="line">&#x27;drf_multiple_model&#x27;,</span><br><span class="line">&#x27;rest_framework&#x27;,</span><br><span class="line">...</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"># 在views.py中使用</span><br><span class="line">    from drf_multiple_model.pagination import MultipleModelLimitOffsetPagination</span><br><span class="line">    from drf_multiple_model.views import ObjectMultipleModelAPIView</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    class LimitPagination(MultipleModelLimitOffsetPagination):</span><br><span class="line">        # 多个models数据表联合查询，分页，每页限制数据10条</span><br><span class="line">        default_limit = 10</span><br><span class="line">        </span><br><span class="line">    class StudentSerializers(serializers.ModelSerializer):</span><br><span class="line">        &quot;&quot;&quot;学生表序列化器&quot;&quot;&quot;</span><br><span class="line">        # merchant = MerchantSerializers()</span><br><span class="line">        </span><br><span class="line">        register_time = serializers.DateTimeField(read_only=True, format=&quot;%Y-%m-%d %H:%M:%S&quot;)</span><br><span class="line">       </span><br><span class="line">        class Meta:</span><br><span class="line">            model = Student</span><br><span class="line">            fields = &#x27;__all__&#x27;</span><br><span class="line">    class ClassesSerializers(serializers.ModelSerializer):</span><br><span class="line">        &quot;&quot;&quot;班级表序列化器&quot;&quot;&quot;</span><br><span class="line">        # merchant = MerchantSerializers()</span><br><span class="line">        </span><br><span class="line">        add_time = serializers.DateTimeField(read_only=True, format=&quot;%Y-%m-%d %H:%M:%S&quot;)</span><br><span class="line">       </span><br><span class="line">        class Meta:</span><br><span class="line">            model = Classes</span><br><span class="line">            fields = &#x27;__all__&#x27;</span><br><span class="line">    </span><br><span class="line">    class SchoolSerializers(serializers.ModelSerializer):</span><br><span class="line">        &quot;&quot;&quot;学校表序列化器&quot;&quot;&quot;</span><br><span class="line">        # merchant = MerchantSerializers()</span><br><span class="line">        </span><br><span class="line">        add_time = serializers.DateTimeField(read_only=True, format=&quot;%Y-%m-%d %H:%M:%S&quot;)</span><br><span class="line">       </span><br><span class="line">        class Meta:</span><br><span class="line">            model = School</span><br><span class="line">            fields = &#x27;__all__&#x27;      </span><br><span class="line">            </span><br><span class="line">    </span><br><span class="line">    class StudentInfo(ObjectMultipleModelAPIView):</span><br><span class="line">        # 获取学生信息，班级信息，学校信息</span><br><span class="line">    </span><br><span class="line">        def get(self, request, *args, **kwargs):</span><br><span class="line">            uid = request.GET.get(&#x27;uid&#x27;, &#x27;&#x27;)  # 学生id</span><br><span class="line">            cid = request.GET.get(&#x27;cid&#x27;, &#x27;&#x27;)  # 班级id</span><br><span class="line">            sid = request.GET.get(&#x27;sid&#x27;, &#x27;&#x27;)  # 学校id</span><br><span class="line">            </span><br><span class="line">            self.querylist = [</span><br><span class="line">                &#123;&#x27;queryset&#x27;: Student.objects.filter(id=uid).order_by(&#x27;-id&#x27;),</span><br><span class="line">                    &#x27;serializer_class&#x27;:  StudentSerializers, &#x27;label&#x27;: &#x27;student&#x27;, &#125;,</span><br><span class="line">                &#123;&#x27;queryset&#x27;: Classes.objects.filter(id=cid).order_by(&#x27;-id&#x27;),</span><br><span class="line">                    &#x27;serializer_class&#x27;:  ClassesSerializers, &#x27;label&#x27;: &#x27;classes&#x27;, &#125;,</span><br><span class="line">                &#123;&#x27;queryset&#x27;: School.objects.filter(id=sid).order_by(&#x27;-id&#x27;),</span><br><span class="line">                    &#x27;serializer_class&#x27;:  SchoolSerializers, &#x27;label&#x27;: &#x27;school&#x27;, &#125;,</span><br><span class="line">            ]</span><br><span class="line">    </span><br><span class="line">            return self.list(request, *args, **kwargs)</span><br><span class="line">    </span><br><span class="line">        pagination_class = LimitPagination</span><br></pre></td></tr></table></figure>
<h2 id="51、-Django序列化器返回外键关联数据"><a href="#51、-Django序列化器返回外键关联数据" class="headerlink" title="51、 Django序列化器返回外键关联数据"></a>51、 Django序列化器返回外键关联数据</h2><p>通过 related_name=’goods_price’把两个表关联起来，当返回Goods的信息时也会返回相应的GoodsPrice信息</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">class GoodsPriceSerializers(serializers.ModelSerializer):</span><br><span class="line">    &quot;&quot;&quot;商品价格表序列化器&quot;&quot;&quot;</span><br><span class="line">    </span><br><span class="line">    class Meta:</span><br><span class="line">        model = GoodsPrice</span><br><span class="line">        fields = [&#x27;price&#x27;]</span><br><span class="line"></span><br><span class="line">class GoodsSerializers(serializers.ModelSerializer):</span><br><span class="line">    &quot;&quot;&quot;商品表序列化器&quot;&quot;&quot;</span><br><span class="line">    </span><br><span class="line">    goods_price = GoodsPriceSerializers(many=True, read_only=True)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        model = Goods</span><br><span class="line">        fields = [&#x27;title&#x27;,&#x27;goods_price&#x27;]</span><br><span class="line">        depth = 2</span><br><span class="line"></span><br><span class="line">class Goods(models.Model):</span><br><span class="line">    &quot;&quot;&quot;商品表&quot;&quot;&quot;</span><br><span class="line">    title = models.CharField(max_length=50, verbose_name=&#x27;商品名称&#x27;)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        db_table = &#x27;goods&#x27;</span><br><span class="line">        verbose_name = &#x27;商品信息表&#x27;</span><br><span class="line">        verbose_name_plural = verbose_name</span><br><span class="line"></span><br><span class="line">    def __str__(self):</span><br><span class="line">        return self.title</span><br><span class="line"></span><br><span class="line">class GoodsPrice(models.Model):</span><br><span class="line">    &quot;&quot;&quot;商品价格表,通过外键关联商品信息表&quot;&quot;&quot;</span><br><span class="line"></span><br><span class="line">    price = models.DecimalField(max_digits=10, decimal_places=2, default=0, verbose_name=&#x27;售价&#x27;)</span><br><span class="line">    goods = models.ForeignKey(to=&#x27;Goods&#x27;, related_name=&#x27;goods_price&#x27;, on_delete=models.SET_NULL, blank=True, null=True,verbose_name=&#x27;商品&#x27;)</span><br><span class="line"></span><br><span class="line">    def __str__(self):</span><br><span class="line">        return str(self.price)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        managed = True</span><br><span class="line">        db_table = &#x27;goodsPrice&#x27;</span><br><span class="line">        verbose_name = &#x27;商品售价&#x27;</span><br><span class="line">        verbose_name_plural = verbose_name</span><br></pre></td></tr></table></figure>
<h2 id="52、python-Django通过User-Agent判断请求来源是微信扫一扫或者是支付宝扫一扫"><a href="#52、python-Django通过User-Agent判断请求来源是微信扫一扫或者是支付宝扫一扫" class="headerlink" title="52、python Django通过User Agent判断请求来源是微信扫一扫或者是支付宝扫一扫"></a>52、python Django通过User Agent判断请求来源是微信扫一扫或者是支付宝扫一扫</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">class Footest(APIView):</span><br><span class="line"> def get(self, request, *args, **kwargs):</span><br><span class="line">     # print(request.META)</span><br><span class="line">     if &#x27;MicroMessenger&#x27; in request.META[&#x27;HTTP_USER_AGENT&#x27;]:</span><br><span class="line">         return Response(data=&#123;&#x27;msg&#x27;: &#x27;访问来源是微信&#x27;&#125;)</span><br><span class="line">     elif &#x27;AlipayClient&#x27; in request.META[&#x27;HTTP_USER_AGENT&#x27;]:</span><br><span class="line">         return Response(data=&#123;&#x27;msg&#x27;: &#x27;访问来源是支付宝&#x27;&#125;)</span><br><span class="line">     else:</span><br><span class="line">         return Response(data=&#123;&#x27;msg&#x27;: &#x27;访问来源是其他&#x27;&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>User Agent中文名为用户代理，简称 UA，它是一个特殊字符串头，使得服务器能够识别客户使用的操作系统及版本、CPU 类型、浏览器及版本、浏览器渲染引擎、浏览器语言、浏览器插件等。</p>
<p>浏览器的 UA 字串</p>
<p>标准格式为： 浏览器标识 (操作系统标识; 加密等级标识; 浏览器语言) 渲染引擎标识 版本信息</p>
<p>获取user-Agent 之后，<br>通过识别MicroMessenger或者AlipayClient这样的关键字应该就可以判断是微信还是支付宝<br>【参考文章】 <a target="_blank" rel="noopener" href="https://blog.csdn.net/fly910905/article/details/82498813?utm_source=blogxgwz4">https://blog.csdn.net/fly910905/article/details/82498813?utm_source=blogxgwz4</a></p>
<h2 id="53、xadmin后台集成’导入‘插件，导入excel文件"><a href="#53、xadmin后台集成’导入‘插件，导入excel文件" class="headerlink" title="53、xadmin后台集成’导入‘插件，导入excel文件"></a>53、xadmin后台集成’导入‘插件，导入excel文件</h2><p>效果图：</p>
<p><img src="https://img-blog.csdnimg.cn/20190426190357134.png" alt="在这里插入图片描述"></p>
<p>1、添加</p>
<p>在虚拟环境根目录\Lib\site-packages\xadmin\plugins中添加excel.py文件</p>
<p><img src="https://img-blog.csdnimg.cn/20190426190440451.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<pre><code>from xadmin.views import BaseAdminPlugin, ListAdminView
from django.template import loader
import xadmin

class ListExcelImportPlugin(BaseAdminPlugin):
    # 重写init_request
    import_excel = False
    def init_request(self, *args, **kwargs):
        return self.import_excel

    def block_top_toolbar(self, context, nodes):
        # 这里 xadmin/excel/model_list.top_toolbar.import.html 是自己写的html文件
      nodes.append(loader.render_to_string(&quot;xadmin/excel/model_list.top_toolbar.import.html&quot;))

xadmin.site.register_plugin(ListExcelImportPlugin, ListAdminView)
</code></pre><p>在虚拟环境根目录\Lib\site-packages\xadmin\plugins<strong>init</strong>.py中</p>
<pre><code>PLUGINS = (
...
    &#39;excel&#39;,
...
)
</code></pre><p>2、添加html文件</p>
<p><img src="https://img-blog.csdnimg.cn/20190426190455263.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>在虚拟环境根目录\Lib\site-packages\xadmin\templates\xadmin\中增加文件夹excel,在文件夹中添加model_list.top_toolbar.import.html文件</p>
<pre><code>    &#123;% load i18n %&#125;
&lt;div class=&quot;btn-group export&quot;&gt;
    &lt;a class=&quot;dropdown-toggle btn btn-default btn-sm&quot; data-toggle=&quot;dropdown&quot; href=&quot;#&quot;&gt;
        &lt;i class=&quot;icon-share&quot;&gt;&lt;/i&gt; 导入数据 &lt;span class=&quot;caret&quot;&gt;&lt;/span&gt;
    &lt;/a&gt;
    &lt;ul class=&quot;dropdown-menu&quot; role=&quot;menu&quot; aria-labelledby=&quot;dLabel&quot;&gt;
        &lt;li&gt;&lt;a data-toggle=&quot;modal&quot; data-target=&quot;#export-modal-import-excel&quot;&gt;&lt;i class=&quot;icon-circle-arrow-down&quot;&gt;&lt;/i&gt; 导入
            Excel&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;

    &lt;div id=&quot;export-modal-import-excel&quot; class=&quot;modal fade&quot;&gt;
        &lt;div class=&quot;modal-dialog&quot;&gt;
            &lt;div class=&quot;modal-content&quot;&gt;
                &lt;form method=&quot;post&quot; action=&quot;&quot; enctype=&quot;multipart/form-data&quot;&gt;
                    &lt;!--&#123;% csrf_token %&#125;--&gt;
                    &lt;div class=&quot;modal-header&quot;&gt;
                        &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-hidden=&quot;true&quot;&gt;×&lt;/button&gt;
                        &lt;h4 class=&quot;modal-title&quot;&gt;导入 Excel&lt;/h4&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;modal-body&quot;&gt;
                        &lt;input type=&quot;file&quot; onchange=&quot;fileChange(this)&quot; name=&quot;excel&quot; id=&quot;submit_upload&quot;&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;modal-footer&quot;&gt;
                        &lt;button type=&quot;button&quot; class=&quot;btn btn-default&quot; data-dismiss=&quot;modal&quot;&gt;&#123;% trans "Close" %&#125;&lt;/button&gt;
                        &lt;button class=&quot;btn btn-success&quot; type=&quot;button&quot; id=&quot;submit_upload_b&quot;&gt;&lt;i class=&quot;icon-share&quot;&gt;&lt;/i&gt; 导入
                        &lt;/button&gt;
                    &lt;/div&gt;
                &lt;/form&gt;
            &lt;/div&gt;&lt;!-- /.modal-content --&gt;
        &lt;/div&gt;&lt;!-- /.modal-dalog --&gt;
    &lt;/div&gt;&lt;!-- /.modal --&gt;

&lt;/div&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
    function fileChange(target) &#123;
        //检测上传文件的类型
        var imgName = document.all.submit_upload.value;
        var ext, idx;
        if (imgName == &#39;&#39;) &#123;
            document.all.submit_upload_b.disabled = true;
            alert(&quot;请选择需要上传的 xls 文件!&quot;);
            return;
        &#125; else &#123;
            idx = imgName.lastIndexOf(&quot;.&quot;);
            if (idx != -1) &#123;
                ext = imgName.substr(idx + 1).toUpperCase();
                ext = ext.toLowerCase();

                if (ext != &#39;xls&#39; &amp;&amp; ext != &#39;xlsx&#39;) &#123;
                    document.all.submit_upload_b.disabled = true;
                    alert(&quot;只能上传 .xls 类型的文件!&quot;);
                    return;
                &#125;
            &#125; else &#123;
                document.all.submit_upload_b.disabled = true;
                alert(&quot;只能上传 .xls 类型的文件!&quot;);
                return;
            &#125;
        &#125;
    &#125;

    $(document).ready(function () &#123;

        $(&#39;#submit_upload_b&#39;).click(function () &#123;
            var form_data = new FormData();
            var file_info = $(&#39;#submit_upload&#39;)[0].files[0];
            form_data.append(&#39;file&#39;, file_info);
            form_data.append(&#39;file_source&#39;, $(&#39;.breadcrumb li&#39;).eq(1).text().trim());
            var url = window.location.protocol + &#39;//&#39; + window.location.host + &#39;/importkdorderno/&#39;
            $.ajax(&#123;
                url: url,
                type: &#39;POST&#39;,
                data: form_data,
                dataType: &quot;json&quot;,
                beforeSend: function (xhr) &#123;
                    xhr.setRequestHeader(&quot;X-CSRFToken&quot;, $.getCookie(&quot;csrftoken&quot;))
                &#125;,
                processData: false,  // tell jquery not to process the data
                contentType: false, // tell jquery not to set contentType
                success: function (res) &#123;

                    alert(res.msg);
                    window.location.reload();
                &#125;,
                error: function (err) &#123;
                &#125;
            &#125;);
        &#125;);
    &#125;)
&lt;/script&gt;
</code></pre><p>3、在views.py处理上传的excel文件</p>
<pre><code>import pandas as pd
from rest_framework.views import APIView

class ImportKDOrderNo(APIView):

    def post(self, request, *args, **kwargs):
        file = request.FILES.get(&#39;file&#39;)
        # read = InMemoryUploadedFile().open()
        data = pd.read_excel(file)  # 使用pandas处理excel文件
        file_source = request.POST.get(&#39;file_source&#39;, &#39;&#39;)  # 文件来源

        if &#39;订单号&#39; and &#39;物流单号&#39; not in data:
            return Response(data=&#123;&#39;msg&#39;: &#39;文件格式有误，第一行第一列应该为【订单号】，第一行第二列应该为【物流单号】&#39;&#125;)
        ordernos = data[&#39;订单号&#39;]
        logistics = data[&#39;物流单号&#39;]
        for i in range(len(ordernos)):
            print(&#39;订单号&#39;, ordernos[i], &#39;物流单号&#39;, logistics[i])

        return Response(data=&#123;&#39;msg&#39;: &#39;上传成功&#39;&#125;)
</code></pre><p>4、在urls.py中添加访问路由</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">from django.urls import path</span><br><span class="line">from 你的应用名称 import views</span><br><span class="line"></span><br><span class="line">app_name = &#x27;你的应用名称&#x27;</span><br><span class="line"></span><br><span class="line">urlpatterns = [</span><br><span class="line">    # 其他路由</span><br><span class="line">    ...</span><br><span class="line">    # 导入物流单号</span><br><span class="line">    path(&#x27;importkdorderno/&#x27;, views.ImportKDOrderNo.as_view(), name=&#x27;importkdorderno&#x27;),</span><br><span class="line">]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="54、Django中查找今天进账金额"><a href="#54、Django中查找今天进账金额" class="headerlink" title="54、Django中查找今天进账金额"></a>54、Django中查找今天进账金额</h2><p>views.py</p>
<pre><code>from datetime import datetime
class CountFee(APIView):
    def get(self, request, *args, **kwargs):
        # 获取当前时间的年月日，然后使用聚合函数添加fee字段的值
        year = datetime.now().year
        month = datetime.now().month
        day = datetime.now().day
        count_fees = FeeDetail.objects.filter(addtime__year=year, addtime__month=month, addtime__day=day).aggregate(Sum(&#39;fee&#39;))
        all_fee = count_fees[&#39;fee__sum&#39;] if count_fees[&#39;fee__sum&#39;] else 0
        print(all_fee)
        return Response(&#123;&#39;code&#39;: 1, &#39;msg&#39;: &#39;success&#39;, &#39;data&#39;: &#123;&#39;all_fee&#39;: all_fee&#125;&#125;)
</code></pre><h2 id="55、判断是什么系统"><a href="#55、判断是什么系统" class="headerlink" title="55、判断是什么系统"></a>55、判断是什么系统</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">import platform</span><br><span class="line">PlATFORM = platform.system()</span><br><span class="line">if PlATFORM == &quot;Linux&quot;:</span><br><span class="line">    print(&#x27;linux&#x27;)</span><br><span class="line">else:</span><br><span class="line">    print(&#x27;其他&#x27;) </span><br></pre></td></tr></table></figure>
<h2 id="56、sql查询"><a href="#56、sql查询" class="headerlink" title="56、sql查询"></a>56、sql查询</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 联合更新</span><br><span class="line">update malluser set master_master_id=3 where master_id in (select a.id from (select id from  malluser where id like &#x27;15%&#x27;)a) </span><br><span class="line"># 统计某字段重复数据</span><br><span class="line">SELECT phone, COUNT(*) AS sumCount FROM malluser GROUP BY phone HAVING sumCount &gt; 1;</span><br></pre></td></tr></table></figure>
<h2 id="57、-xadmin后台删除数据出现错误"><a href="#57、-xadmin后台删除数据出现错误" class="headerlink" title="57、 xadmin后台删除数据出现错误"></a>57、 xadmin后台删除数据出现错误</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">`get_deleted_objects() takes 3 positional arguments but 5 were given`</span><br></pre></td></tr></table></figure>
<p>这是由于Django2.1版本和xadmin不兼容导致的</p>
<p>知道虚拟环境\Lib\site-packages\xadmin\plugins\actions.py</p>
<p>修改93行，</p>
<p>把</p>
<pre><code>deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
            queryset, self.opts, self.user, self.admin_site, using)
</code></pre><p>改为</p>
<pre><code>deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
            queryset, self.user, self.admin_site)
</code></pre><p>然后在adminx.py文件中对应的模型类中允许删除</p>
<pre><code>class MaterialAdmin(object):
    &quot;&quot;&quot;素材库分类&quot;&quot;&quot;
    list_display = [&#39;id&#39;, &#39;name&#39;, &#39;class_id&#39;, &#39;is_delete&#39;, &#39;addtime&#39;]

    def has_delete_permission(self, *args, **kwargs):
        return True
</code></pre><h2 id="58、xdamin限制用户点击"><a href="#58、xdamin限制用户点击" class="headerlink" title="58、xdamin限制用户点击"></a>58、xdamin限制用户点击</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//如果登录z=xadmin后台的账号不是【root】的就不能点击更新操作</span><br><span class="line">    var master_name = $(&#x27;#top-nav&#x27;).find(&#x27;strong&#x27;).text();</span><br><span class="line">        master_name =  master_name.substring(4);</span><br><span class="line">    if(master_name != &#x27;root&#x27;)&#123;</span><br><span class="line">            $(&quot;.grid-item a&quot;).each(function(index, element) &#123;</span><br><span class="line">                $(this).attr(&#x27;href&#x27;,&#x27;#&#x27;);</span><br><span class="line">            &#125;);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<h2 id="59、获取公众号关注url"><a href="#59、获取公众号关注url" class="headerlink" title="59、获取公众号关注url"></a>59、获取公众号关注url</h2><p>在微信网页版，打开公众号，点击右上角“…”，在弹框中选择右下角中间的“查看历史记录”，然后在弹框中选择左上角倒数第一个，“用默认浏览器打开”，就可以在打开的浏览器中获取该公众号的关注url，当把这个url发给好友时，好友点开的就是去关注公众号的页面。</p>
<h2 id="60、xadmin后台用户操作表权限"><a href="#60、xadmin后台用户操作表权限" class="headerlink" title="60、xadmin后台用户操作表权限"></a>60、xadmin后台用户操作表权限</h2><p>虚拟环境根目录\Lib\site-packages\xadmin\views\base.py</p>
<p>可以找到：</p>
<p><img src="https://img-blog.csdnimg.cn/20190627140512311.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>在项目子应用下的adminx.py中使用</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">import xadmin</span><br><span class="line"></span><br><span class="line">from machine.models import Machine</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">class MachineAdmin(object):</span><br><span class="line">    list_display = [&#x27;code&#x27;,]  # 显示的字段</span><br><span class="line"></span><br><span class="line">    search_fields = [&#x27;code&#x27;]  # 搜索的字段</span><br><span class="line">    list_filter = [&#x27;code&#x27;, &#x27;is_delete&#x27;] # 过滤的字段</span><br><span class="line">    ordering = (&#x27;-id&#x27;,) # 按id降序排序</span><br><span class="line">    list_editable = [&#x27;is_delete&#x27;, ]  # 数据即时编辑</span><br><span class="line">    list_per_page = 30  # 每页显示数据数量</span><br><span class="line">    model_icon = &#x27;fa fa-cog fa-spin&#x27;  # 左侧显示的小图标</span><br><span class="line"></span><br><span class="line">    def has_delete_permission(self, *args, **kwargs): </span><br><span class="line">        # 删除权限</span><br><span class="line">        if self.request.user.is_superuser:  # 管理员才能增加</span><br><span class="line">            return True</span><br><span class="line">        return False</span><br><span class="line"></span><br><span class="line">    def has_add_permission(self, *args, **kwargs):</span><br><span class="line">        if self.request.user.is_superuser:  # 管理员才能增加</span><br><span class="line">            return True</span><br><span class="line">        return False</span><br><span class="line"></span><br><span class="line">    def has_change_permission(self, *args, **kwargs):</span><br><span class="line">        if self.request.user.is_superuser: # 管理员才能修改</span><br><span class="line">            return True</span><br><span class="line">        return False</span><br><span class="line"></span><br><span class="line">    def queryset(self):</span><br><span class="line">        qs = super(MachineAdmin, self).queryset()</span><br><span class="line">        if self.request.user.is_superuser:  # 管理员可以查看所有</span><br><span class="line">            return qs</span><br><span class="line">        else:</span><br><span class="line">            # 登录用户只能看到自己修改的数据</span><br><span class="line">            return qs.filter(master_id=self.request.user.last_name)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">xadmin.site.register(MallMachine, MallMachineAdmin)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="61、使用nginx部署项目"><a href="#61、使用nginx部署项目" class="headerlink" title="61、使用nginx部署项目"></a>61、使用nginx部署项目</h2><p>先在/etc/nginx/sites-available中创建一个配置文件，文件名为test(注意没有后缀):</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">#设定虚拟主机配置</span><br><span class="line">    server &#123;</span><br><span class="line">        #侦听80端口</span><br><span class="line">        listen 80;</span><br><span class="line">        listen 443 ssl;</span><br><span class="line">        #定义使用 www.nginx.cn访问</span><br><span class="line">		#ssl on;</span><br><span class="line">        server_name  xxx.xxx.com;</span><br><span class="line">        #定义服务器的默认网站根目录位置</span><br><span class="line">        root /root/项目名称;</span><br><span class="line">		ssl_session_timeout 5m; </span><br><span class="line">        ssl_certificate   /etc/nginx/cert/xxx.pem;</span><br><span class="line">        ssl_certificate_key  /etc/nginx/cert/xxx.key;</span><br><span class="line">        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;</span><br><span class="line">        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;</span><br><span class="line">        ssl_prefer_server_ciphers on;</span><br><span class="line">        #设定本虚拟主机的访问日志</span><br><span class="line">        #access_log  logs/nginx.access.log  main;</span><br><span class="line"> </span><br><span class="line">        #默认请求</span><br><span class="line">        location / &#123;</span><br><span class="line">            #倒入了uwsgi的配置</span><br><span class="line">            include uwsgi_params; </span><br><span class="line">		    client_max_body_size	50m;</span><br><span class="line">            #连接uwsgi的超时时间</span><br><span class="line">           # uwsgi_connect_timeout 30; </span><br><span class="line"> 	    #设定了uwsig服务器位置</span><br><span class="line"> 	    	uwsgi_pass 127.0.0.1:8002;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        location /static&#123;</span><br><span class="line">      	alias /root/项目名称/static;</span><br><span class="line">        &#125;</span><br><span class="line">	location /media &#123;</span><br><span class="line">	alias /root/项目名称/media;</span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>其中xxx.xxx.com表示域名.如果没有https，就使用#把ssl注释掉就可以了。</p>
<p>然后把test映射到/etc/nginx/sites-enabled</p>
<p>命令</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ln -s /etc/nginx/sites-available/test /etc/nginx/sites-enabled/test</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>即可<br>注意：<br>uwsgi中配置listen=1024时，启动uwsgi时可能会报错：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">django + uwsgi + nginx 日志Listen queue size is greater than the system max net.core.somaxconn (128).</span><br></pre></td></tr></table></figure>
<p>解决方法:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">修改系统参数</span><br><span class="line"></span><br><span class="line">/proc/sys/net/ipv4/tcp_max_syn_backlog  原来2048    改为8192</span><br><span class="line">/proc/sys/net/core/somaxconn	原来128     改为262144</span><br></pre></td></tr></table></figure>
<p>重启nginx</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nginx -s reload</span><br></pre></td></tr></table></figure>
<h2 id="62、xadmin后台发送邮件找回密码"><a href="#62、xadmin后台发送邮件找回密码" class="headerlink" title="62、xadmin后台发送邮件找回密码"></a>62、xadmin后台发送邮件找回密码</h2><p><img src="https://img-blog.csdnimg.cn/20190710230444358.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>输入你用户绑定的邮箱</p>
<p><img src="https://img-blog.csdnimg.cn/20190710230454725.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>想要发送邮件，需要在settings.py中设置邮件发送器</p>
<p>settings.py最下面增加</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"># ------------------------邮箱配置-----------------------------------------</span><br><span class="line">EMAIL_BACKEND = &#x27;django.core.mail.backends.smtp.EmailBackend&#x27; #把要发送的邮件显示再控制台上，方便调试</span><br><span class="line">EMAIL_USE_SSL = True</span><br><span class="line">EMAIL_HOST = &#x27;smtp.qq.com&#x27;  # 如果是 163 改成 smtp.163.com</span><br><span class="line">EMAIL_PORT = 465</span><br><span class="line">EMAIL_HOST_USER = &#x27;邮箱账号&#x27; # 帐号</span><br><span class="line">EMAIL_HOST_PASSWORD = &#x27;授权码&#x27;  # 到邮箱里开通</span><br><span class="line">DEFAULT_FROM_EMAIL = EMAIL_HOST_USER</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>由于django2与xadmin有些地方不兼容，需要修改源码：</p>
<p>找到虚拟环境根目录\Lib\site-packages\xadmin\plugins\passwords.py</p>
<p>在passwords.py文件中大概79行，修改为</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">return password_reset_confirm(request=request, uidb36=uidb36, token=token,</span><br><span class="line">                                      template_name=self.password_reset_confirm_template,</span><br><span class="line">                                      token_generator=self.password_reset_token_generator,</span><br><span class="line">                                      set_password_form=self.password_reset_set_form,</span><br><span class="line">                                      post_reset_redirect=self.get_admin_url(&#x27;xadmin_password_reset_complete&#x27;),</span><br><span class="line">                                      current_app=self.admin_site.name, extra_context=context).dispatch(request=request,</span><br><span class="line">                                                                                                        uidb64=uidb36,token=token)</span><br><span class="line">             </span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>找到虚拟环境根目录Lib\site-packages\django\contrib\auth\views.py</p>
<p>在views.py文件中大概258行，增加：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># 成功后跳转路由,根据自己实际来定</span><br><span class="line">self.success_url = self.request.build_absolute_uri(&#x27;/&#x27;) + &#x27;xadmin/&#x27;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/2020052620533440.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h2 id="63、xadmin外键下拉框添加过滤"><a href="#63、xadmin外键下拉框添加过滤" class="headerlink" title="63、xadmin外键下拉框添加过滤"></a>63、xadmin外键下拉框添加过滤</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">class MallGoodsAdmin(object):</span><br><span class="line">    &quot;&quot;&quot;商品管理&quot;&quot;&quot;</span><br><span class="line">    list_display = [&#x27;id&#x27;, &#x27;show_photo&#x27;, &#x27;nickname&#x27;, &#x27;merchant&#x27;, &#x27;goods_class&#x27;, &#x27;label&#x27;,]</span><br><span class="line">    search_fields = [&#x27;nickname&#x27;]</span><br><span class="line">    list_filter = [&#x27;goods_class&#x27;, &#x27;label&#x27;,]</span><br><span class="line">    model_icon = &#x27;fa fa-bars&#x27;</span><br><span class="line">    list_editable = [&#x27;goods_class&#x27;, ]</span><br><span class="line">    #,重写虚拟环境根目录下\Lib\site-packages\xadmin\views\edit.py中的formfield_for_dbfield</span><br><span class="line">    def formfield_for_dbfield(self, db_field, **kwargs):</span><br><span class="line">        # 对MallGoodsClass这个表项的下拉框选择进行过滤</span><br><span class="line">        # MallGoods中有一个goods_class商品分类外键MallGoodsClass，过滤掉外键MallGoodsClass中 </span><br><span class="line">        # master_class为空的值</span><br><span class="line">        if db_field.name == &quot;goods_class&quot;:</span><br><span class="line">            kwargs[&quot;queryset&quot;] = MallGoodsClass.objects.filter(master_class__isnull=False)</span><br><span class="line">            # 对assigned_recipient这个表项的下拉选择进行过滤</span><br><span class="line">        	return db_field.formfield(**dict(**kwargs))</span><br><span class="line">        return super().formfield_for_dbfield(db_field, **kwargs)</span><br><span class="line"></span><br><span class="line">xadmin.site.register(models.MallGoods, MallGoodsAdmin)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="64、xadmin即时编辑器去掉空标签"><a href="#64、xadmin即时编辑器去掉空标签" class="headerlink" title="64、xadmin即时编辑器去掉空标签"></a>64、xadmin即时编辑器去掉空标签</h2><p><img src="https://img-blog.csdnimg.cn/20190716151440115.png" alt="在这里插入图片描述"></p>
<p>虚拟环境根目录下\Lib\site-packages\xadmin\plugins\editable.py，在大概</p>
<p>129行增加：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">form.fields[fields[0]].empty_label = None</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20190716151459680.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h2 id="65、用户增加的小组件，让其他用户可见"><a href="#65、用户增加的小组件，让其他用户可见" class="headerlink" title="65、用户增加的小组件，让其他用户可见"></a>65、用户增加的小组件，让其他用户可见</h2><p>找到虚拟环境根目录\Lib\site-packages\xadmin\views\dashboard.py</p>
<p>在548行、554行</p>
<p><img src="https://img-blog.csdnimg.cn/20190723175537371.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>改为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">@filter_hook</span><br><span class="line">    def get_widgets(self):</span><br><span class="line"></span><br><span class="line">        if self.widget_customiz:</span><br><span class="line">            portal_pos = UserSettings.objects.filter(</span><br><span class="line">               key=self.get_portal_key())</span><br><span class="line">            if len(portal_pos):</span><br><span class="line">                portal_pos = portal_pos[0].value</span><br><span class="line">                widgets = []</span><br><span class="line"></span><br><span class="line">                if portal_pos:</span><br><span class="line">                    user_widgets = dict([(uw.id, uw) for uw in UserWidget.objects.filter(page_id=self.get_page_id())])</span><br><span class="line">                    for col in portal_pos.split(&#x27;|&#x27;):</span><br><span class="line">                        ws = []</span><br><span class="line">                        for wid in col.split(&#x27;,&#x27;):</span><br><span class="line">                            try:</span><br><span class="line">                                widget = user_widgets.get(int(wid))</span><br><span class="line">                                if widget:</span><br><span class="line">                                    ws.append(self.get_widget(widget))</span><br><span class="line">                            except Exception as e:</span><br><span class="line">                                import logging</span><br><span class="line">                                logging.error(e, exc_info=True)</span><br><span class="line">                        widgets.append(ws)</span><br><span class="line"></span><br><span class="line">                return widgets</span><br><span class="line"></span><br><span class="line">        return self.get_init_widget()</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="66、pip-install-uwsgi出错"><a href="#66、pip-install-uwsgi出错" class="headerlink" title="66、pip install uwsgi出错"></a>66、pip install uwsgi出错</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>首先安装python3环境</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt install python3-dev</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>然后再虚拟环境中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">pip install uwsgi</span><br></pre></td></tr></table></figure>
<h2 id="66、xadmin后台加载数据慢，解决方案"><a href="#66、xadmin后台加载数据慢，解决方案" class="headerlink" title="66、xadmin后台加载数据慢，解决方案"></a>66、xadmin后台加载数据慢，解决方案</h2><p>list_filter: 过滤器要慎用，不要使用类似id这些数据量大的字段</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">class MallUserAdmin(object):</span><br><span class="line">    &quot;&quot;&quot;用户管理&quot;&quot;&quot;</span><br><span class="line"></span><br><span class="line">    list_display = [&#x27;id&#x27;, &#x27;tp_icon&#x27;, &#x27;nickname&#x27;, &#x27;phone&#x27;, &#x27;level&#x27;, &#x27;balance&#x27;, &#x27;province&#x27;, &#x27;city&#x27;, &#x27;quxian&#x27;]  # 显示字段</span><br><span class="line">    search_fields = [&#x27;id&#x27;, &#x27;nickname&#x27;, &#x27;phone&#x27;]  # 搜索</span><br><span class="line">    list_filter = [&#x27;level&#x27;, &#x27;province&#x27;, &#x27;city&#x27;, &#x27;quxian&#x27;]  # 过滤器</span><br><span class="line">    # list_filter = [&#x27;id&#x27;, &#x27;level&#x27;, &#x27;province&#x27;, &#x27;city&#x27;, &#x27;quxian&#x27;]  # 如果加id，xadmin加载回来的数据就会很慢，所以不要在过滤器上使用id</span><br><span class="line">    list_per_page = 30  # 默认每页数量</span><br><span class="line">    model_icon = &#x27;fa fa-users&#x27;  # 左侧图标</span><br><span class="line">    ordering = [&#x27;-id&#x27;]  # 排序</span><br><span class="line">    readonly_fields = [&#x27;subscribe&#x27;, &#x27;wx_openid&#x27;, &#x27;phone&#x27;]  # 只读字段</span><br><span class="line">    is_addbalance = True   # 加载自定义的插件</span><br><span class="line">    relfield_style = &#x27;fk-ajax&#x27;  # 其他表如果外键到用户表就做ajax搜索查询，不一次性加载数据</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="67-、xadmin导出插件处理，增加导出勾选数据项"><a href="#67-、xadmin导出插件处理，增加导出勾选数据项" class="headerlink" title="67 、xadmin导出插件处理，增加导出勾选数据项"></a>67 、xadmin导出插件处理，增加导出勾选数据项</h2><p>常规的导出只有两个选择【导出表头】、【导出全部数据】</p>
<p><img src="https://img-blog.csdnimg.cn/20190917141511283.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>现在想要做的是增加一个选择，即【导出表头】、【导出全部数据】、【导出勾选数据】，如下图：</p>
<p><img src="https://img-blog.csdnimg.cn/20190917141839822.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>需要修改xadmin源代码，具体如下</p>
<h4 id="1、加载js文件"><a href="#1、加载js文件" class="headerlink" title="1、加载js文件"></a>1、加载js文件</h4><p>找到虚拟环境\Lib\site-packages\xadmin\views\list.py，在607行增加’xadmin.plugin.importexport.js’，如下图所示</p>
<p><img src="https://img-blog.csdnimg.cn/20190917141853832.png" alt="在这里插入图片描述"></p>
<h4 id="2、修改export-py，后端处理下载文件"><a href="#2、修改export-py，后端处理下载文件" class="headerlink" title="2、修改export.py，后端处理下载文件"></a>2、修改export.py，后端处理下载文件</h4><p>找到虚拟环境\Lib\site-packages\xadmin\plugins\export.py</p>
<p>在84行把rows = context[‘results’]修改成如下函数</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"># 新增导出所选数据</span><br><span class="line"># rows = context[&#x27;results&#x27;]  </span><br><span class="line">rows = []</span><br><span class="line">select_across = self.request.GET.get(&#x27;_select_across&#x27;, False) == &#x27;1&#x27;</span><br><span class="line">selected = self.request.GET.get(&#x27;_selected_actions&#x27;, &#x27;&#x27;)</span><br><span class="line">if self.request.GET.get(&#x27;selected&#x27;, &#x27;off&#x27;) == &#x27;on&#x27;:</span><br><span class="line">    if not select_across:</span><br><span class="line">        selected_pk = selected.split(&#x27;,&#x27;)</span><br><span class="line">        for i in context[&#x27;results&#x27;]:</span><br><span class="line">            if str(i[&#x27;object&#x27;].id) in selected_pk:</span><br><span class="line">                rows.append(i)</span><br><span class="line">    else:</span><br><span class="line">        rows = context[&#x27;results&#x27;]</span><br><span class="line">else:</span><br><span class="line">    rows = context[&#x27;results&#x27;]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/2019091714200418.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>3、 修改model_list.top_toolbar.exports.html<br>找到虚拟环境\Lib\site-packages\xadmin\templates\xadmin\blocks\model_list.top_toolbar.exports.html</p>
<p>使用以下代码覆盖原文件</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line">&#123;% load i18n %&#125;</span><br><span class="line">&lt;div class=&quot;btn-group export&quot;&gt;</span><br><span class="line">    &lt;a id=&quot;export-menu&quot; class=&quot;dropdown-toggle btn btn-default btn-sm&quot; data-toggle=&quot;dropdown&quot; href=&quot;#&quot;&gt;</span><br><span class="line">        &lt;i class=&quot;fa fa-share&quot;&gt;&lt;/i&gt; &#123;% trans &quot;Export&quot; %&#125; &lt;span class=&quot;caret&quot;&gt;&lt;/span&gt;</span><br><span class="line">    &lt;/a&gt;</span><br><span class="line">    &lt;ul class=&quot;dropdown-menu&quot; role=&quot;menu&quot; aria-labelledby=&quot;dLabel&quot;&gt;</span><br><span class="line">        &#123;% for et in export_types %&#125;</span><br><span class="line">        &lt;li&gt;&lt;a data-toggle=&quot;modal&quot; data-target=&quot;#export-modal-&#123;&#123;et.type&#125;&#125;&quot;&gt;&lt;i class=&quot;fa fa-arrow-circle-down&quot;&gt;</span><br><span class="line">        &lt;/i&gt; &#123;% trans &quot;Export&quot; %&#125; &#123;&#123;et.name&#125;&#125;&lt;/a&gt;&lt;/li&gt;</span><br><span class="line">        &#123;% endfor %&#125;</span><br><span class="line">    &lt;/ul&gt;</span><br><span class="line"></span><br><span class="line">    &#123;% for et in export_types %&#125;</span><br><span class="line">    &lt;div id=&quot;export-modal-&#123;&#123;et.type&#125;&#125;&quot; class=&quot;modal fade&quot;&gt;</span><br><span class="line">        &lt;div class=&quot;modal-dialog&quot;&gt;</span><br><span class="line">            &lt;div class=&quot;modal-content&quot;&gt;</span><br><span class="line">                &lt;form method=&quot;get&quot; action=&quot;&quot;&gt;</span><br><span class="line"></span><br><span class="line">                    &lt;div class=&quot;modal-header&quot;&gt;</span><br><span class="line">                        &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/button&gt;</span><br><span class="line">                        &lt;h4 class=&quot;modal-title&quot;&gt;&#123;% trans &quot;Export&quot; %&#125; &#123;&#123;et.name&#125;&#125;&lt;/h4&gt;</span><br><span class="line">                    &lt;/div&gt;</span><br><span class="line">                    &lt;div class=&quot;modal-body&quot;&gt;</span><br><span class="line">                        &#123;&#123; form_params|safe &#125;&#125;</span><br><span class="line">                        &lt;input type=&quot;hidden&quot; name=&quot;export_type&quot; value=&quot;&#123;&#123;et.type&#125;&#125;&quot;&gt;</span><br><span class="line">                        &lt;!-- 增加 导出所选数据 一栏 --&gt;</span><br><span class="line">                        &lt;input type=&quot;hidden&quot; name=&quot;_selected_actions&quot; value=&quot;&quot;/&gt;</span><br><span class="line">                        &lt;input type=&quot;hidden&quot; name=&quot;_select_across&quot; value=&quot;&quot;/&gt;</span><br><span class="line"></span><br><span class="line">                        &lt;label class=&quot;checkbox&quot;&gt;</span><br><span class="line">                            &#123;% if et.type == &quot;xlsx&quot; %&#125;</span><br><span class="line">                            &lt;input type=&quot;checkbox&quot; name=&quot;export_xlsx_header&quot; checked=&quot;checked&quot; value=&quot;on&quot;&gt;</span><br><span class="line">                            &#123;% trans &quot;Export with table header.&quot; %&#125;</span><br><span class="line">                            &#123;% endif %&#125;</span><br><span class="line">                            &#123;% if et.type == &quot;xls&quot; %&#125;</span><br><span class="line">                            &lt;input type=&quot;checkbox&quot; name=&quot;export_xls_header&quot; checked=&quot;checked&quot; value=&quot;on&quot;&gt;</span><br><span class="line">                            &#123;% trans &quot;Export with table header.&quot; %&#125;</span><br><span class="line">                            &#123;% endif %&#125;</span><br><span class="line">                            &#123;% if et.type == &quot;csv&quot; %&#125;</span><br><span class="line">                            &lt;input type=&quot;checkbox&quot; name=&quot;export_csv_header&quot; checked=&quot;checked&quot; value=&quot;on&quot;&gt;</span><br><span class="line">                            &#123;% trans &quot;Export with table header.&quot; %&#125;</span><br><span class="line">                            &#123;% endif %&#125;</span><br><span class="line">                            &#123;% if et.type == &quot;xml&quot; %&#125;</span><br><span class="line">                            &lt;input type=&quot;checkbox&quot; name=&quot;export_xml_format&quot; checked=&quot;checked&quot; value=&quot;on&quot;&gt;</span><br><span class="line">                            &#123;% trans &quot;Export with format.&quot; %&#125;</span><br><span class="line">                            &#123;% endif %&#125;</span><br><span class="line">                            &#123;% if et.type == &quot;json&quot; %&#125;</span><br><span class="line">                            &lt;input type=&quot;checkbox&quot; name=&quot;export_json_format&quot; checked=&quot;checked&quot; value=&quot;on&quot;&gt;</span><br><span class="line">                            &#123;% trans &quot;Export with format.&quot; %&#125;</span><br><span class="line">                            &#123;% endif %&#125;</span><br><span class="line">                        &lt;/label&gt;</span><br><span class="line">                        &lt;label class=&quot;checkbox&quot;&gt;</span><br><span class="line">                            &lt;input type=&quot;checkbox&quot; name=&quot;all&quot; value=&quot;on&quot;&gt; &#123;% trans &quot;Export all data.&quot; %&#125;</span><br><span class="line">                        &lt;/label&gt;</span><br><span class="line">                        &lt;!-- 增加 导出所选数据 一栏 --&gt;</span><br><span class="line">                        &lt;label class=&quot;checkbox&quot;&gt;</span><br><span class="line">                            &lt;input type=&quot;checkbox&quot; name=&quot;selected&quot; value=&quot;on&quot;&gt; 导出勾选数据</span><br><span class="line">                        &lt;/label&gt;</span><br><span class="line"></span><br><span class="line">                    &lt;/div&gt;</span><br><span class="line">                    &lt;div class=&quot;modal-footer&quot;&gt;</span><br><span class="line">                        &lt;button type=&quot;button&quot; class=&quot;btn btn-default&quot; data-dismiss=&quot;modal&quot;&gt;&#123;% trans &quot;Close&quot; %&#125;&lt;/button&gt;</span><br><span class="line"></span><br><span class="line">                        &lt;button class=&quot;btn btn-success myexport  glyphicon glyphicon-export &quot; type=&quot;submit&quot;&gt;&lt;i</span><br><span class="line">                                class=&quot;fa fa-share&quot;&gt;&lt;/i&gt; &#123;% trans &quot;Export&quot; %&#125;</span><br><span class="line">                        &lt;/button&gt;</span><br><span class="line">                    &lt;/div&gt;</span><br><span class="line">                &lt;/form&gt;</span><br><span class="line">            &lt;/div&gt;&lt;!-- /.modal-content --&gt;</span><br><span class="line">        &lt;/div&gt;&lt;!-- /.modal-dalog --&gt;</span><br><span class="line">    &lt;/div&gt;&lt;!-- /.modal --&gt;</span><br><span class="line">    &#123;% endfor %&#125;</span><br><span class="line"></span><br><span class="line">&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">&lt;script type=&quot;text/javascript&quot;&gt;</span><br><span class="line">    // 如果是订单导出，把待出货订单设置成待收货订单</span><br><span class="line">    $(document).ready(function () &#123;</span><br><span class="line">        $(&#x27;.myexport&#x27;).click(function () &#123;</span><br><span class="line">            // 当把订单导出时，需要修改订单状态为待收货状态</span><br><span class="line">            var url = window.location.protocol + &#x27;//&#x27; + window.location.host + &quot;/exportorder/&quot;;</span><br><span class="line">            $(&quot;input[name=&#x27;_select_across&#x27;]&quot;).val($(&quot;input[name=&#x27;select_across&#x27;]&quot;).val());</span><br><span class="line">            if ($(&quot;input[name=&#x27;selected&#x27;]&quot;).is(&#x27;:checked&#x27;)) &#123;</span><br><span class="line">                var arr = [];</span><br><span class="line">                $.each($(&#x27;.action-select&#x27;), function () &#123;</span><br><span class="line">                    if (true == $(this).prop(&#x27;checked&#x27;)) &#123;</span><br><span class="line">                        arr.push($(this).val());</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;);</span><br><span class="line">                if(arr.length == 0)&#123;</span><br><span class="line">                    alert(&#x27;请先勾选导出数据&#x27;)</span><br><span class="line">                    return false</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;else&#123;</span><br><span class="line">                var arr = []</span><br><span class="line">                var order_type = $(&#x27;.breadcrumb li&#x27;).eq(1).text().trim()</span><br><span class="line">                $(&#x27;.grid-item&#x27;).each(function (index, el) &#123;</span><br><span class="line"></span><br><span class="line">                    arr.push($(el).find(&#x27;td&#x27;).eq(1).text().trim())</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;</span><br><span class="line">            if (($(&#x27;.breadcrumb &gt; li&#x27;).eq(1).text()).indexOf(&#x27;订单&#x27;) != -1) &#123;</span><br><span class="line">                // 5秒后执行</span><br><span class="line">                setTimeout(function () &#123;</span><br><span class="line">                    $.ajax(&#123;</span><br><span class="line">                        type: &quot;POST&quot;,</span><br><span class="line">                        url: url,</span><br><span class="line">                        data: &#123;&#x27;orderlist&#x27;: JSON.stringify(arr), &#x27;order_type&#x27;: order_type,&#125;,</span><br><span class="line">                        beforeSend: function (xhr) &#123;</span><br><span class="line">                            xhr.setRequestHeader(&quot;X-CSRFToken&quot;, $.getCookie(&quot;csrftoken&quot;))</span><br><span class="line">                        &#125;,</span><br><span class="line">                        success: function (data) &#123;</span><br><span class="line">                            window.location.reload();</span><br><span class="line">                        &#125;,</span><br><span class="line">                        error: function (xhr) &#123;</span><br><span class="line">                            alert(&quot;出现未知错误&quot;);</span><br><span class="line">                            window.location.reload();</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;);</span><br><span class="line">                &#125;, 5000);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;)</span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="68、使用F查询更新用户重要数据"><a href="#68、使用F查询更新用户重要数据" class="headerlink" title="68、使用F查询更新用户重要数据"></a>68、使用F查询更新用户重要数据</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">from django.db.models import F</span><br><span class="line"># 使用F查询更新用户余额</span><br><span class="line">balance = 5  </span><br><span class="line">MallUser.objects.filter(id=1).update(balance=F(&#x27;balance&#x27;) + balance)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>相当于sql的</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">update Malluser set balance=balance + 5 where id=1;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="69、日志输出模块"><a href="#69、日志输出模块" class="headerlink" title="69、日志输出模块"></a>69、日志输出模块</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">import logging</span><br><span class="line">import platform</span><br><span class="line"># 全局函数</span><br><span class="line">PlATFORM = platform.system()</span><br><span class="line">if (PlATFORM == &quot;Linux&quot;):</span><br><span class="line">    # linux系统,文件保存在var下</span><br><span class="line">    SERVER_LOG_PATH = &#x27;/var/mylog.log&#x27;</span><br><span class="line">else:</span><br><span class="line">    # windows系统,文件保存在D盘下</span><br><span class="line">    SERVER_LOG_PATH = &#x27;D:\mylog.log&#x27;</span><br><span class="line"># 定义一个logging的对象，命名为mylog</span><br><span class="line">LOGGER = logging.getLogger(&#x27;mylog&#x27;)</span><br><span class="line"># 设置级别为WARNING</span><br><span class="line">LOGGER.setLevel(logging.WARNING)</span><br><span class="line"># 创建一个handler，用于写入日志文件</span><br><span class="line">fh = logging.FileHandler(SERVER_LOG_PATH, encoding=&#x27;utf-8&#x27;)</span><br><span class="line">fh.setLevel(logging.WARNING)</span><br><span class="line"># 定义handler的输出格式</span><br><span class="line">formatter = logging.Formatter(&#x27;%(asctime)s - %(name)s - %(levelname)s - %(message)s&#x27;, datefmt=&#x27;%Y-%m-%d %H:%M:%S&#x27;)</span><br><span class="line">fh.setFormatter(formatter)</span><br><span class="line"># 给Logger添加handler</span><br><span class="line">LOGGER.addHandler(fh)</span><br><span class="line"># 不在控制台显示</span><br><span class="line">LOGGER.propagate = False</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="70、字典排序"><a href="#70、字典排序" class="headerlink" title="70、字典排序"></a>70、字典排序</h2><p>字典在内存中发布是无序的，当想对键值或者键名进行排序时可以先把字典转成元组，这可以达到排序的目的。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">score = &#123;&#x27;小明&#x27;: &#123;&#x27;avg_score&#x27;: 90, &#x27;English&#x27;: 90, &#x27;Math&#x27;: 90, &#x27;Chniese&#x27;: 90, &#125;,</span><br><span class="line">     &#x27;小红&#x27;: &#123;&#x27;avg_score&#x27;: 60, &#x27;English&#x27;: 60, &#x27;Math&#x27;: 61, &#x27;Chniese&#x27;: 59, &#125;,</span><br><span class="line">     &#x27;小黑&#x27;: &#123;&#x27;avg_score&#x27;: 70, &#x27;English&#x27;: 75, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 70, &#125;,</span><br><span class="line">     &#x27;小白&#x27;: &#123;&#x27;avg_score&#x27;: 80, &#x27;English&#x27;: 95, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 80, &#125;,</span><br><span class="line">     &#125;</span><br><span class="line"># 对姓名进行排序，即对键名进行排序</span><br><span class="line">b = sorted(score.items(), key=lambda x: x[0], reverse=True)</span><br><span class="line">show_str = &#x27;&#x27;</span><br><span class="line">for info in b:</span><br><span class="line">    # print(info)</span><br><span class="line">    key, value = info[0], info[1]</span><br><span class="line">    show_str += &#x27;姓名：&#123;&#125;,平均分：&#123;&#125;，成绩：&#123;&#125;&#x27;.format(key,value[&#x27;avg_score&#x27;], value) + &#x27;\r\n&#x27;</span><br><span class="line">print(&#x27;对姓名进行排序&#x27;)</span><br><span class="line">print(show_str)</span><br><span class="line"># 对平均分进行排序</span><br><span class="line">b = sorted(score.items(), key=lambda x: x[1][&#x27;avg_score&#x27;], reverse=True)</span><br><span class="line">show_str = &#x27;&#x27;</span><br><span class="line">for info in b:</span><br><span class="line">    # print(info)</span><br><span class="line">    key, value = info[0], info[1]</span><br><span class="line">    show_str += &#x27;姓名：&#123;&#125;,平均分：&#123;&#125;，成绩：&#123;&#125;&#x27;.format(key,value[&#x27;avg_score&#x27;], value) + &#x27;\r\n&#x27;</span><br><span class="line">print(&#x27;对平均分进行排序&#x27;)</span><br><span class="line">print(show_str)</span><br><span class="line"></span><br><span class="line"># 对英语成绩进行排序</span><br><span class="line">b = sorted(score.items(), key=lambda x: x[1][&#x27;English&#x27;], reverse=True)</span><br><span class="line">show_str = &#x27;&#x27;</span><br><span class="line">for info in b:</span><br><span class="line">    # print(info)</span><br><span class="line">    key, value = info[0], info[1]</span><br><span class="line">    show_str += &#x27;姓名：&#123;&#125;,平均分：&#123;&#125;，成绩：&#123;&#125;&#x27;.format(key,value[&#x27;avg_score&#x27;], value) + &#x27;\r\n&#x27;</span><br><span class="line">print(&#x27;对英语成绩进行排序&#x27;)</span><br><span class="line">print(show_str)</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">对姓名进行排序</span><br><span class="line">姓名：小黑,平均分：70，成绩：&#123;&#x27;avg_score&#x27;: 70, &#x27;English&#x27;: 75, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 70&#125;</span><br><span class="line">姓名：小红,平均分：60，成绩：&#123;&#x27;avg_score&#x27;: 60, &#x27;English&#x27;: 60, &#x27;Math&#x27;: 61, &#x27;Chniese&#x27;: 59&#125;</span><br><span class="line">姓名：小白,平均分：80，成绩：&#123;&#x27;avg_score&#x27;: 80, &#x27;English&#x27;: 95, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 80&#125;</span><br><span class="line">姓名：小明,平均分：90，成绩：&#123;&#x27;avg_score&#x27;: 90, &#x27;English&#x27;: 90, &#x27;Math&#x27;: 90, &#x27;Chniese&#x27;: 90&#125;</span><br><span class="line"></span><br><span class="line">对平均分进行排序</span><br><span class="line">姓名：小明,平均分：90，成绩：&#123;&#x27;avg_score&#x27;: 90, &#x27;English&#x27;: 90, &#x27;Math&#x27;: 90, &#x27;Chniese&#x27;: 90&#125;</span><br><span class="line">姓名：小白,平均分：80，成绩：&#123;&#x27;avg_score&#x27;: 80, &#x27;English&#x27;: 95, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 80&#125;</span><br><span class="line">姓名：小黑,平均分：70，成绩：&#123;&#x27;avg_score&#x27;: 70, &#x27;English&#x27;: 75, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 70&#125;</span><br><span class="line">姓名：小红,平均分：60，成绩：&#123;&#x27;avg_score&#x27;: 60, &#x27;English&#x27;: 60, &#x27;Math&#x27;: 61, &#x27;Chniese&#x27;: 59&#125;</span><br><span class="line"></span><br><span class="line">对英语成绩进行排序</span><br><span class="line">姓名：小白,平均分：80，成绩：&#123;&#x27;avg_score&#x27;: 80, &#x27;English&#x27;: 95, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 80&#125;</span><br><span class="line">姓名：小明,平均分：90，成绩：&#123;&#x27;avg_score&#x27;: 90, &#x27;English&#x27;: 90, &#x27;Math&#x27;: 90, &#x27;Chniese&#x27;: 90&#125;</span><br><span class="line">姓名：小黑,平均分：70，成绩：&#123;&#x27;avg_score&#x27;: 70, &#x27;English&#x27;: 75, &#x27;Math&#x27;: 65, &#x27;Chniese&#x27;: 70&#125;</span><br><span class="line">姓名：小红,平均分：60，成绩：&#123;&#x27;avg_score&#x27;: 60, &#x27;English&#x27;: 60, &#x27;Math&#x27;: 61, &#x27;Chniese&#x27;: 59&#125;</span><br></pre></td></tr></table></figure>
<h3 id="对列表中字典按多个键值排序"><a href="#对列表中字典按多个键值排序" class="headerlink" title="对列表中字典按多个键值排序"></a>对列表中字典按多个键值排序</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">from operator import itemgetter</span><br><span class="line">data = [</span><br><span class="line">    &#123;&#x27;code&#x27;: 1, &#x27;position&#x27;: 300, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;,</span><br><span class="line">    &#123;&#x27;code&#x27;: 2, &#x27;position&#x27;: 255, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;,</span><br><span class="line">    &#123;&#x27;code&#x27;: 3, &#x27;position&#x27;: 256, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;,</span><br><span class="line">    &#123;&#x27;code&#x27;: 1, &#x27;position&#x27;: 500, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;,</span><br><span class="line">    &#123;&#x27;code&#x27;: 5, &#x27;position&#x27;: 455, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;,</span><br><span class="line">    &#123;&#x27;code&#x27;: 1, &#x27;position&#x27;: 322, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;,</span><br><span class="line">    &#123;&#x27;code&#x27;: 6, &#x27;position&#x27;: 676, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;,</span><br><span class="line">]</span><br><span class="line">data = sorted(data, key=itemgetter(&#x27;code&#x27;, &#x27;position&#x27;))</span><br><span class="line"></span><br><span class="line">print(data)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">&#123;&#x27;code&#x27;: 1, &#x27;position&#x27;: 300, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;, </span><br><span class="line">&#123;&#x27;code&#x27;: 1, &#x27;position&#x27;: 322, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;, </span><br><span class="line">&#123;&#x27;code&#x27;: 1, &#x27;position&#x27;: 500, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;, </span><br><span class="line">&#123;&#x27;code&#x27;: 2, &#x27;position&#x27;: 255, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;, </span><br><span class="line">&#123;&#x27;code&#x27;: 3, &#x27;position&#x27;: 256, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;, </span><br><span class="line">&#123;&#x27;code&#x27;: 5, &#x27;position&#x27;: 455, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;, </span><br><span class="line">&#123;&#x27;code&#x27;: 6, &#x27;position&#x27;: 676, &#x27;time&#x27;: &#x27;1620442242&#x27;&#125;]</span><br></pre></td></tr></table></figure>
<h2 id="71、时间格式与字符串互转、比较大小"><a href="#71、时间格式与字符串互转、比较大小" class="headerlink" title="71、时间格式与字符串互转、比较大小"></a>71、时间格式与字符串互转、比较大小</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">import datetime</span><br><span class="line"></span><br><span class="line"># 当前时间转字符串</span><br><span class="line">now = datetime.datetime.strftime(datetime.datetime.now(), &#x27;%Y-%m-%d %H:%M:%S&#x27;)</span><br><span class="line"># 字符串转时间格式</span><br><span class="line">now = datetime.datetime.strptime(now, &#x27;%Y-%m-%d %H:%M:%S&#x27;)</span><br><span class="line">a = now + datetime.timedelta(minutes=-15)</span><br><span class="line"># 时间比较大小</span><br><span class="line">if a &lt; now:</span><br><span class="line">    print(&#x27;yes&#x27;)</span><br></pre></td></tr></table></figure>
<h2 id="72、python把’-u’开头的字符串转中文"><a href="#72、python把’-u’开头的字符串转中文" class="headerlink" title="72、python把’\u’开头的字符串转中文"></a>72、python把’\u’开头的字符串转中文</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">str_data = &#x27;\\u7528\\u6237 ID \\u6216\\u5bc6\\u7801\\u9519\\u8bef&#x27;</span><br><span class="line"># 字符串.encode(&#x27;utf-8&#x27;).decode(&#x27;unicode_escape&#x27;)</span><br><span class="line">str_data_to_zh = str_data.encode(&#x27;utf-8&#x27;).decode(&#x27;unicode_escape&#x27;)</span><br><span class="line">print(str_data_to_zh)</span><br><span class="line"></span><br><span class="line"># 输出</span><br><span class="line">用户 ID 或密码错误</span><br></pre></td></tr></table></figure>
<h2 id="73、django进行数据迁移时出现No-changes-detected解决方案"><a href="#73、django进行数据迁移时出现No-changes-detected解决方案" class="headerlink" title="73、django进行数据迁移时出现No changes detected解决方案"></a>73、django进行数据迁移时出现No changes detected解决方案</h2><p>原因：可能是由于app下面没有migrations文件夹<br>所以需要创建这个文件夹，命令</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python manage.py makemigrations --empty 你的app名称</span><br></pre></td></tr></table></figure>
<h2 id="74、ubuntu下载文件到windows桌面"><a href="#74、ubuntu下载文件到windows桌面" class="headerlink" title="74、ubuntu下载文件到windows桌面"></a>74、ubuntu下载文件到windows桌面</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt install lrzsz</span><br><span class="line">sz 123.txt</span><br></pre></td></tr></table></figure>
<h2 id="75、git查看提交日志"><a href="#75、git查看提交日志" class="headerlink" title="75、git查看提交日志"></a>75、git查看提交日志</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git log --author=&quot;ldc&quot;</span><br></pre></td></tr></table></figure>
<h2 id="76、python翻译模块"><a href="#76、python翻译模块" class="headerlink" title="76、python翻译模块"></a>76、python翻译模块</h2><p>可以把英文翻译成中文，也可以把中文翻译成英文</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">pip install  translate</span><br><span class="line"></span><br><span class="line">from translate import Translator</span><br><span class="line"></span><br><span class="line">name = &#x27;giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca&#x27;</span><br><span class="line">translator = Translator(to_lang=&quot;chinese&quot;)</span><br><span class="line">translation = translator.translate(name)</span><br><span class="line">print(translation)</span><br><span class="line"></span><br><span class="line">translator= Translator(from_lang=&quot;chinese&quot;,to_lang=&quot;english&quot;)</span><br><span class="line">translation = translator.translate(&quot;我想你&quot;)</span><br><span class="line">print(translation)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"></span><br><span class="line">大熊猫，熊猫，熊猫熊，浣熊，大熊猫</span><br><span class="line">I missed you.</span><br></pre></td></tr></table></figure>
<h2 id="77、python实现字符串转字典"><a href="#77、python实现字符串转字典" class="headerlink" title="77、python实现字符串转字典"></a>77、python实现字符串转字典</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">import ast</span><br><span class="line">import json</span><br><span class="line"></span><br><span class="line">target_str = &#x27;&#123;&quot;h&quot;: 1, &quot;e&quot;: 2, &quot;l&quot;: 3, &quot;l&quot;: 4, &quot;o&quot;: 5&#125;&#x27;</span><br><span class="line">target_str2 = &quot;&#123;&#x27;h&#x27;: 1, &#x27;e&#x27;: 2, &#x27;l&#x27;: 3, &#x27;l&#x27;: 4, &#x27;o&#x27;: 5&#125;&quot;</span><br><span class="line"></span><br><span class="line"># 方式1：使用json，缺点，字符串中不能出现单引号</span><br><span class="line"># 由于 json 语法规定 数组或对象之中的字符串必须使用双引号，不能使用单引号</span><br><span class="line"># 官网https://www.json.org/json-en.html上有一段描述是</span><br><span class="line"># A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes</span><br><span class="line">print(json.loads(target_str))</span><br><span class="line"># print(json.loads(target_str2))   # 使用json转这个字符串会报错 Expecting property name enclosed in double quotes</span><br><span class="line"></span><br><span class="line"># 方式2：使用eval函数，缺点，不安全</span><br><span class="line">print(eval(target_str))</span><br><span class="line">print(eval(target_str2))</span><br><span class="line"># print(eval(&quot;__import__(&#x27;os&#x27;).system(&#x27;dir&#x27;)&quot;))  # 会列出当前的目录文件，如果字符串是一些删除命令，则可以把整个目录清空！</span><br><span class="line"></span><br><span class="line"># 方式3，使用ast.literal_eval，没有json与eval的缺点，推荐使用这个</span><br><span class="line">print(ast.literal_eval(target_str))</span><br><span class="line">print(ast.literal_eval(target_str2))</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#x27;h&#x27;: 1, &#x27;e&#x27;: 2, &#x27;l&#x27;: 4, &#x27;o&#x27;: 5&#125;</span><br><span class="line">&#123;&#x27;h&#x27;: 1, &#x27;e&#x27;: 2, &#x27;l&#x27;: 4, &#x27;o&#x27;: 5&#125;</span><br><span class="line">&#123;&#x27;h&#x27;: 1, &#x27;e&#x27;: 2, &#x27;l&#x27;: 4, &#x27;o&#x27;: 5&#125;</span><br><span class="line">&#123;&#x27;h&#x27;: 1, &#x27;e&#x27;: 2, &#x27;l&#x27;: 4, &#x27;o&#x27;: 5&#125;</span><br><span class="line">&#123;&#x27;h&#x27;: 1, &#x27;e&#x27;: 2, &#x27;l&#x27;: 4, &#x27;o&#x27;: 5&#125;</span><br></pre></td></tr></table></figure>
<h2 id="78、django-app-如何在后台显示中文名"><a href="#78、django-app-如何在后台显示中文名" class="headerlink" title="78、django app 如何在后台显示中文名"></a>78、django app 如何在后台显示中文名</h2><p>1.在app (这里以user为例)下面的<strong>init</strong>.py文件中</p>
<p>添加:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">default_app_config = &#x27;user.apps.UserConfig&#x27;</span><br></pre></td></tr></table></figure>
<p>2.在apps.py中</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">from django.apps import AppConfig</span><br><span class="line"></span><br><span class="line">class UserConfig(AppConfig):</span><br><span class="line">    name = &#x27;user&#x27;</span><br><span class="line">    verbose_name = &#x27;用户&#x27;</span><br></pre></td></tr></table></figure>
<h2 id="79、使用python给Excel指定行添加数据"><a href="#79、使用python给Excel指定行添加数据" class="headerlink" title="79、使用python给Excel指定行添加数据"></a>79、使用python给Excel指定行添加数据</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">import openpyxl, sys</span><br><span class="line"></span><br><span class="line">wb = openpyxl.load_workbook(&#x27;b.xlsx&#x27;)</span><br><span class="line">sheet = wb[&#x27;Sheet1&#x27;]</span><br><span class="line"></span><br><span class="line"># 在excel表格第二行添加新数据</span><br><span class="line">addrow = 1   # 增加一行</span><br><span class="line">row = 2  # 在第二行新增一行</span><br><span class="line">name = &#x27;b-back.xlsx&#x27; # 新的表名</span><br><span class="line"></span><br><span class="line">wb1 = openpyxl.Workbook()</span><br><span class="line">sheet1 = wb1[&#x27;Sheet&#x27;]</span><br><span class="line"></span><br><span class="line"># 复制前row行</span><br><span class="line">for i in range(1, row):</span><br><span class="line">    for j in range(1, sheet.max_column + 1):</span><br><span class="line">        sheet1.cell(row=i, column=j).value = sheet.cell(row=i, column=j).value</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 复制后row行</span><br><span class="line">for i in range(row, sheet.max_row + 1):</span><br><span class="line">    for j in range(1, sheet.max_column + 1):</span><br><span class="line">        if i == row:</span><br><span class="line">            # 给第row行添加新的数据</span><br><span class="line">            sheet1.cell(row=row, column=j).value = &#x27;新增&#x27;</span><br><span class="line">        sheet1.cell(row=i + addrow, column=j).value = sheet.cell(row=i, column=j).value</span><br><span class="line"></span><br><span class="line">wb1.save(name)</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200526120005871.png" alt="在这里插入图片描述"></p>
<h2 id="80、python中的format格式拼接字符串"><a href="#80、python中的format格式拼接字符串" class="headerlink" title="80、python中的format格式拼接字符串"></a>80、python中的format格式拼接字符串</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">d = &#123;&#x27;a&#x27;: 1, &#x27;b&#x27;: 2, &#x27;c&#x27;: 3, &#x27;d&#x27;: 5&#125;</span><br><span class="line"></span><br><span class="line">print(&#x27;&#123;a&#125;,&#123;b&#125;&#x27;.format(**d))</span><br><span class="line">print(&#x27;&#123;0&#125;,&#123;1&#125;,&#123;0&#125;&#x27;.format(&#x27;a&#x27;, &#x27;b&#x27;))</span><br><span class="line">print(f&quot;&#123;d[&#x27;a&#x27;]&#125;&quot;)</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">1,2</span><br><span class="line">a,b,a</span><br><span class="line">1</span><br></pre></td></tr></table></figure>
<h2 id="81、数据库inner-join、full-join、left-join、union、union-all区别"><a href="#81、数据库inner-join、full-join、left-join、union、union-all区别" class="headerlink" title="81、数据库inner join、full join、left join、union、union all区别"></a>81、数据库inner join、full join、left join、union、union all区别</h2><p>表a_test</p>
<p><img src="https://img-blog.csdnimg.cn/20200529102527295.png#pic_center" alt="在这里插入图片描述"></p>
<p>表b_test</p>
<p><img src="https://img-blog.csdnimg.cn/20200529102549669.png#pic_center" alt="在这里插入图片描述"></p>
<p>INNER JOIN</p>
<p>是A和B的交集</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">SELECT * FROM a_test INNER JOIN b_test ON a_test.name = b_test.namesa</span><br><span class="line"></span><br><span class="line">Inner join 产生的结果集中，是A和B的交集。</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102631494.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<p>FULL OUTER JOIN</p>
<p>产生A和B的并集</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">SELECT * FROM a_test FULL OUTER JOIN b_test ON a_test.name = b_test.name </span><br><span class="line"></span><br><span class="line">Full outer join 产生A和B的并集。</span><br><span class="line">但是需要注意的是，对于没有匹配的记录，则会以null做为值。</span><br><span class="line">可以使用IF NULL判断。</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102702865.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">SELECT * FROM a_test FULL OUTER JOIN b_test ON a_test.name = b_test.name</span><br><span class="line">WHERE a_test.id IS null OR b_test.id IS null</span><br><span class="line"></span><br><span class="line">产生A表和B表没有交集的数据集。</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102724731.png#pic_center" alt="在这里插入图片描述"></p>
<p>LEFT [OUTER] JOIN</p>
<p>产生表A的完全集，而B表中匹配的则有值，没有匹配的则以null值取代</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">SELECT * FROM a_test LEFT OUTER JOIN b_test ON a_test.name = b_test.name</span><br><span class="line">有些数据库可以不要OUTER</span><br><span class="line">SELECT * FROM a_test LEFT  JOIN b_test ON a_test.name = b_test.name</span><br><span class="line">Left outer join 产生表A的完全集，而B表中匹配的则有值，没有匹配的则以null值取代。</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102808618.png#pic_center" alt="在这里插入图片描述"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">SELECT * FROM a_test LEFT OUTER JOIN b_test ON a_test.name = b_test.name </span><br><span class="line">WHERE b_test.id IS null</span><br><span class="line"></span><br><span class="line">产生在A表中有而在B表中没有的集合。</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102828846.png#pic_center" alt="在这里插入图片描述"></p>
<p>RIGHT [OUTER] JOIN</p>
<p>RIGHT OUTER JOIN 是后面的表为基础，与LEFT OUTER JOIN用法类似。</p>
<p>UNION 与 UNION ALL</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">UNION 操作符用于合并两个或多个 SELECT 语句的结果集。</span><br><span class="line">请注意，UNION 内部的 SELECT 语句必须拥有相同数量的列。</span><br><span class="line">列也必须拥有相似的数据类型。</span><br><span class="line">同时，每条 SELECT 语句中的列的顺序必须相同。</span><br><span class="line">UNION 只选取记录，而UNION ALL会列出所有记录。</span><br><span class="line"></span><br><span class="line">SELECT name FROM a_test UNION SELECT name FROM b_test</span><br><span class="line"></span><br><span class="line">选取不同值</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102855430.png#pic_center" alt="在这里插入图片描述"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">SELECT a_test.id,a_test.name  FROM a_test </span><br><span class="line">UNION </span><br><span class="line">SELECT b_test.id,b_test.name FROM b_test</span><br><span class="line"></span><br><span class="line">由于 id 51 xh   与 id 4 xh 并不相同，不合并</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102917488.png#pic_center" alt="在这里插入图片描述"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">SELECT name FROM a_test UNION ALL SELECT name FROM b_test</span><br><span class="line"></span><br><span class="line">全部列出来</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200529102937801.png#pic_center" alt="在这里插入图片描述"></p>
<p>还需要注意的是我们还有一个是“交差集” cross join, 这种Join没有办法用文式图表示，因为其就是把表A和表B的数据进行一个N*M的组合，即笛卡尔积。</p>
<p>表达式如下：SELECT * FROM a_test CROSS JOIN b_test</p>
<p>这个笛卡尔乘积会产生 4 x 4 = 16 条记录，一般来说，我们很少用到这个语法。但是我们得小心，如果不是使用嵌套的select语句，一般系统都会产生笛卡尔乘积然再做过滤。这是对于性能来说是非常危险的，尤其是表很大的时候。</p>
<h2 id="82、windows电脑查看端口使用情况"><a href="#82、windows电脑查看端口使用情况" class="headerlink" title="82、windows电脑查看端口使用情况"></a>82、windows电脑查看端口使用情况</h2><p>找出8000端口对应的PID进程，命令为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">netstat -ano|findstr 8000</span><br></pre></td></tr></table></figure>
<p>进程信息如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">TCP    0.0.0.0:8000           0.0.0.0:0              LISTENING       1620</span><br><span class="line">UDP    0.0.0.0:8000           *:*                                    1620</span><br></pre></td></tr></table></figure>
<p>找出进程对应的详细信息：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">tasklist |findstr 1620</span><br><span class="line"></span><br><span class="line">KGService.exe                 1620 Console                    1     18,696 K</span><br></pre></td></tr></table></figure>
<p>关进程：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">taskkill /pid 1620 /F</span><br></pre></td></tr></table></figure>
<p>查看程序占用内存<br>比如查看python占用运行内存</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tasklist  /fi &quot;imagename eq python.exe&quot;</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/a61afd069e964e6f9d755c5c1884ca6f.png" alt="在这里插入图片描述"></p>
<h2 id="83、Django生成表和反向生成Model"><a href="#83、Django生成表和反向生成Model" class="headerlink" title="83、Django生成表和反向生成Model"></a>83、Django生成表和反向生成Model</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">正向生成：</span><br><span class="line">python manage.py makemigrations</span><br><span class="line">python manage.py migrate</span><br><span class="line"></span><br><span class="line">反向：</span><br><span class="line">会根据设置的数据库中的表在自动生成对应的Model代码，并打印出来</span><br><span class="line">python manage.py inspectdb</span><br><span class="line"></span><br><span class="line">以直接将打印的代码直接导入到指定的Model文件中</span><br><span class="line">python manage.py inspectdb &gt; models.py</span><br></pre></td></tr></table></figure>
<h2 id="84、windows的hosts文件位置"><a href="#84、windows的hosts文件位置" class="headerlink" title="84、windows的hosts文件位置"></a>84、windows的hosts文件位置</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">C:\Windows\System32\drivers\etc\hosts</span><br></pre></td></tr></table></figure>
<h2 id="85、postgresql数据库"><a href="#85、postgresql数据库" class="headerlink" title="85、postgresql数据库"></a>85、postgresql数据库</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br></pre></td><td class="code"><pre><span class="line"># 如果表存在就先删除</span><br><span class="line">drop table if exists student;</span><br><span class="line"># 创建学生表</span><br><span class="line"># id      serial not null  表示id自增</span><br><span class="line"># id      integer not null  表示id不自增</span><br><span class="line">create table student</span><br><span class="line">(</span><br><span class="line">    id                    serial not null</span><br><span class="line">        constraint student_pk</span><br><span class="line">            primary key,</span><br><span class="line">    name                  varchar,</span><br><span class="line">    class_id              integer,</span><br><span class="line">    height                numeric,</span><br><span class="line">    weight                numeric,</span><br><span class="line">    write_date            timestamp</span><br><span class="line">    </span><br><span class="line">);</span><br><span class="line"></span><br><span class="line">comment on table student is &#x27;学生表&#x27;;</span><br><span class="line"></span><br><span class="line">comment on column student.name is &#x27;名称&#x27;;</span><br><span class="line"></span><br><span class="line">comment on column student.class_id is &#x27;班级ID&#x27;;</span><br><span class="line"></span><br><span class="line">comment on column student.height is &#x27;身高&#x27;;</span><br><span class="line"></span><br><span class="line">comment on column student.weight is &#x27;体重&#x27;;</span><br><span class="line"></span><br><span class="line">comment on column student.write_date is &#x27;修改时间&#x27;;</span><br><span class="line"></span><br><span class="line">alter table student</span><br><span class="line">    owner to odoo;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 更新</span><br><span class="line">update user set name=&#x27;张三&#x27; where id=111</span><br><span class="line"></span><br><span class="line"># 更新表a的字段为表b的值</span><br><span class="line">update account_invoice </span><br><span class="line">set sale_order_id=so.id </span><br><span class="line">from sale_order so </span><br><span class="line">where account_invoice.origin=so.name</span><br><span class="line"></span><br><span class="line"># 新增</span><br><span class="line">insert into &quot;user&quot; (name,sex) values (&#x27;小明&#x27;,1),(&#x27;小红&#x27;, 0)</span><br><span class="line"># 新增或更新</span><br><span class="line">如果id冲突就更新</span><br><span class="line">INSERT INTO  student(id, name)</span><br><span class="line">   VALUES(12, &#x27;小明&#x27;),(13, &#x27;小红&#x27;)</span><br><span class="line">   ON conflict(id) DO UPDATE</span><br><span class="line">   SET name =&#x27;未知&#x27;;</span><br><span class="line">如果id冲突就什么也不做</span><br><span class="line">INSERT INTO  student(id, name)</span><br><span class="line">   VALUES(12, &#x27;小明&#x27;),(13, &#x27;小红&#x27;)</span><br><span class="line">   ON conflict(id) DO NOTHING;</span><br><span class="line"></span><br><span class="line"># 把一个表中的数据插入到另一个表中</span><br><span class="line">insert into 目标表名 (column1,column2,columnn) select value1,value2,valuen from  源表名</span><br><span class="line">比如：</span><br><span class="line">insert into student (name, classs_name,create_date) select  student_name as name, class_name, now() from class_table;</span><br><span class="line"></span><br><span class="line"># 获取当前时间</span><br><span class="line">now()</span><br><span class="line">select now()</span><br><span class="line"></span><br><span class="line"># 删除</span><br><span class="line">DELETE FROM table_name [WHERE Clause]</span><br><span class="line"></span><br><span class="line"># 增加字段</span><br><span class="line">alter table 表名 add column 列名 类型;</span><br><span class="line"># 删除字段</span><br><span class="line">alter table 表名 dropcolumn 列名 ;</span><br><span class="line"></span><br><span class="line"># postgresql数据库查看表所有字段</span><br><span class="line">select * from information_schema.columns where table_schema=&#x27;public&#x27; and table_name=&#x27;表名&#x27;;</span><br><span class="line"></span><br><span class="line"># postgresql获取所有表名</span><br><span class="line">select tablename from pg_tables where schemaname=&#x27;public&#x27;</span><br><span class="line"></span><br><span class="line"># 对查询结果按id降序显示</span><br><span class="line">select * from table_name order by id desc</span><br><span class="line"></span><br><span class="line"># 对查询结果按id升序显示</span><br><span class="line">select * from table_name order by id asc</span><br><span class="line"></span><br><span class="line"># 转义字符, 查找name中包含单引号的记录</span><br><span class="line">select * from student where name like  E&#x27;%\&#x27;%&#x27;;</span><br><span class="line"></span><br><span class="line"># 查看表记录总数</span><br><span class="line">select relname as TABLE_NAME, reltuples as rowCounts from pg_class where relkind = &#x27;r&#x27; and relnamespace = (select oid from pg_namespace where nspname=&#x27;public&#x27;) order by rowCounts desc;</span><br><span class="line"></span><br><span class="line"># 将结果转换为数组</span><br><span class="line">SELECT array(SELECT &quot;name&quot; FROM sale_order);</span><br><span class="line"></span><br><span class="line"># 将数组合并为字符串</span><br><span class="line">select array_to_string(array[1,2,3], &#x27;,&#x27;)</span><br><span class="line"></span><br><span class="line"># 联合子集更新，把sale_order_line的name连接换行符，然后按id更新到表a_test中对应的name</span><br><span class="line">update a_test set name=array_to_string(array(select name from sale_order_line where order_id=a_test.id),&#x27;&lt;br/&gt;&#x27;);</span><br><span class="line"></span><br><span class="line"># PostgreSQL合并查询同一列的多条记录，针对一对多，多对多字段</span><br><span class="line">比如表：</span><br><span class="line"></span><br><span class="line">id   name                  </span><br><span class="line">1    小明                   </span><br><span class="line">1    小红                     id   name</span><br><span class="line">1    小亮        --&gt;          1    小明，小亮，小红</span><br><span class="line">2    小强                     2    小强，小王</span><br><span class="line">2    小王</span><br><span class="line"></span><br><span class="line">SELECT </span><br><span class="line">    id, array_to_string(ARRAY(SELECT unnest(array_agg(name order by name desc))),&#x27;,&#x27;) AS all_name</span><br><span class="line">FROM  </span><br><span class="line">    student</span><br><span class="line">GROUP BY id;</span><br><span class="line"></span><br><span class="line"># case语句</span><br><span class="line">case var</span><br><span class="line">	when condition1 then statement1</span><br><span class="line">	when condition2 then statement2</span><br><span class="line">else statementn</span><br><span class="line">end as new_name</span><br><span class="line"></span><br><span class="line">比如：</span><br><span class="line">select case name</span><br><span class="line">	when &#x27;小明&#x27; then &#x27;xm&#x27;</span><br><span class="line">	when &#x27;小红&#x27; then &#x27;xh&#x27;</span><br><span class="line">	when &#x27;小刚&#x27; then &#x27;xg&#x27;</span><br><span class="line">else &#x27;xw&#x27;</span><br><span class="line">end as short_name</span><br><span class="line">from student</span><br><span class="line"></span><br><span class="line"># 临时表、字符串合并、类型转换、时间格式转换、当前时间</span><br><span class="line">WITH TEMP AS ( </span><br><span class="line">	SELECT CAST ( concat ( bam.account_period_code, &#x27;-01&#x27; ) AS TIMESTAMP ) </span><br><span class="line">	   AS account_period_time </span><br><span class="line">		 FROM bi_account_move AS bam ) </span><br><span class="line">SELECT</span><br><span class="line">	account_period_time,</span><br><span class="line">	to_char(CURRENT_DATE,&#x27;yyyy-MM-dd hh24:MI:ss&#x27;) as current_date,</span><br><span class="line">	to_char( account_period_time, &#x27;yyyy&#x27; ) as year,</span><br><span class="line">	to_char( account_period_time, &#x27;MM&#x27; ) as month,</span><br><span class="line">	to_char( account_period_time, &#x27;dd&#x27; ) as day </span><br><span class="line">FROM TEMP</span><br><span class="line"></span><br><span class="line">比如：</span><br><span class="line">account_period_time	current_date	    year	month	day</span><br><span class="line">2019-06-01 0:00:00	2020-06-24 00:00:00	2019	 06	     01</span><br><span class="line">2019-06-01 0:00:00	2020-06-24 00:00:00	2019	 06	     01</span><br><span class="line"></span><br><span class="line"># 多个字符串拼接</span><br><span class="line">select </span><br><span class="line">concat(&#x27;payment_&#x27;, CAST(ap.id as varchar)) as line_key</span><br><span class="line">from ap</span><br><span class="line"></span><br><span class="line"># 将查询所得结果集的某一字段拼接成字符串，默认的是逗号</span><br><span class="line">select GROUP_CONCAT(id) from test where id&gt;5;</span><br><span class="line">结果为：</span><br><span class="line">1,2,3,4,5,6,7</span><br><span class="line"># 使用其他分隔符拼接</span><br><span class="line">select GROUP_CONCAT(id separator &#x27;-&#x27;) from test where id&gt;5;</span><br><span class="line">结果为：</span><br><span class="line">1-2-3-4-5-6-7</span><br><span class="line"># 多个临时表</span><br><span class="line">WITH temp_student AS ( SELECT ID, NAME, sex FROM student WHERE sex = TRUE ),</span><br><span class="line">temp_class AS (</span><br><span class="line">	SELECT</span><br><span class="line">		ID,</span><br><span class="line">		NAME,</span><br><span class="line">		student_id,</span><br><span class="line">		teacher_id</span><br><span class="line">	FROM</span><br><span class="line">		the_class </span><br><span class="line">	),</span><br><span class="line">	temp_teacher AS (</span><br><span class="line">	SELECT </span><br><span class="line">	  ID,</span><br><span class="line">		NAME,</span><br><span class="line">		age </span><br><span class="line">	FROM</span><br><span class="line">		teacher </span><br><span class="line">	) </span><br><span class="line">	SELECT</span><br><span class="line">	ts.NAME AS student_name,</span><br><span class="line">	tc.NAME AS class_name,</span><br><span class="line">	te.NAME AS teacher_name</span><br><span class="line">	from temp_student as ts</span><br><span class="line">	LEFT JOIN temp_class AS tc ON tc.student_id = ts.ID  </span><br><span class="line">	LEFT JOIN teacher AS te ON te.id = tc.teacher_id  </span><br><span class="line"></span><br><span class="line"># null转成有意义的值</span><br><span class="line">select COALESCE(name, &#x27;&#x27;) as name from a_test</span><br><span class="line">意思就是如果name为null，就转为空字符串</span><br><span class="line"></span><br><span class="line"># 字符串截取</span><br><span class="line">select SUBSTRING(&#x27;abcd&#x27;,1,2); -- result:ab 表示从下标从1开始,截取2个字符</span><br><span class="line"></span><br><span class="line"># 使用 interval 时间相加减(+/-)</span><br><span class="line">当前时间 + 10秒，</span><br><span class="line">select to_char(now() + interval &#x27;10 second&#x27;, &#x27;yyyy-mm-dd hh24:mi:ss&#x27;)  as reqDate from account_period;</span><br><span class="line">当前时间 - 10秒</span><br><span class="line">select to_char(now() + interval &#x27;-10 second&#x27;, &#x27;yyyy-mm-dd hh24:mi:ss&#x27;)  as reqDate from account_period;</span><br><span class="line"> </span><br><span class="line">当前时间 + 10分，</span><br><span class="line">select to_char(now() + interval &#x27;10 minute&#x27;, &#x27;yyyy-mm-dd hh24:mi:ss&#x27;)  as reqDate  from account_period;</span><br><span class="line"> </span><br><span class="line">当前时间 + 10时，</span><br><span class="line">select to_char(now() + interval &#x27;10 hour&#x27;, &#x27;yyyy-mm-dd hh24:mi:ss&#x27;)  as reqDate from account_period;</span><br><span class="line"> </span><br><span class="line">当前时间 + 10天，</span><br><span class="line">select to_char(now() + interval &#x27;10 day&#x27;, &#x27;yyyy-mm-dd hh24:mi:ss&#x27;)  as reqDate from account_period;</span><br><span class="line"></span><br><span class="line">当前时间 + 10年，</span><br><span class="line">select to_char(now() + interval &#x27;10 year&#x27;, &#x27;yyyy-mm-dd hh24:mi:ss&#x27;)  as reqDate from account_period;</span><br><span class="line"></span><br><span class="line"># UNION types numeric and text cannot be matched</span><br><span class="line">NULL::NUMERIC</span><br><span class="line"> 这个问题，是因为几个sql组合在一起时，同一个字段的值，遇到null时，需要进行类型转换。</span><br><span class="line"></span><br><span class="line"># 对行记录定义行编号，使用函数ROW_NUMBER()</span><br><span class="line"># 按分数进行降序，然后给行记录标记行编号，可以作为排名来使用</span><br><span class="line">select           </span><br><span class="line">	ROW_NUMBER() OVER (ORDER BY score desc) AS sequence_number,</span><br><span class="line">	name,score</span><br><span class="line">from</span><br><span class="line">	b_test</span><br><span class="line"></span><br><span class="line"># 数字转字符串  </span><br><span class="line">select cast(123 as VARCHAR); </span><br><span class="line"></span><br><span class="line"># 字符串转数字  </span><br><span class="line">select cast(&#x27;123&#x27; as INTEGER);</span><br></pre></td></tr></table></figure>
<h2 id="86、python控制台输出带颜色的文字方法"><a href="#86、python控制台输出带颜色的文字方法" class="headerlink" title="86、python控制台输出带颜色的文字方法"></a>86、python控制台输出带颜色的文字方法</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">#格式：　　设置颜色开始 ：\033[显示方式;前景色;背景色m</span><br><span class="line">#说明：</span><br><span class="line">前景色            背景色           颜色</span><br><span class="line">---------------------------------------</span><br><span class="line">30                40              黑色</span><br><span class="line">31                41              红色</span><br><span class="line">32                42              绿色</span><br><span class="line">33                43              黃色</span><br><span class="line">34                44              蓝色</span><br><span class="line">35                45              紫红色</span><br><span class="line">36                46              青蓝色</span><br><span class="line">37                47              白色</span><br><span class="line">显示方式           意义</span><br><span class="line">-------------------------</span><br><span class="line">0                终端默认设置</span><br><span class="line">1                高亮显示</span><br><span class="line">4                使用下划线</span><br><span class="line">5                闪烁</span><br><span class="line">7                反白显示</span><br><span class="line">8                不可见</span><br><span class="line"> </span><br><span class="line">#例子：</span><br><span class="line">\033[1;31;40m    &lt;!--1-高亮显示 31-前景色红色  40-背景色黑色--&gt;</span><br><span class="line">\033[0m          &lt;!--采用终端默认设置，即取消颜色设置--&gt;</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">例子</span><br><span class="line">print(&#x27;紫红字体 \033[1;35m hello world \033[0m!&#x27;)</span><br><span class="line">print(&#x27;褐色背景绿色字体 \033[1;32;43m hello world \033[0m!&#x27;)</span><br><span class="line">print(&#x27;\033[1;33;44mhello world\033[0m&#x27;)</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200615143709116.png" alt="在这里插入图片描述"></p>
<h2 id="87、控制台输出白色方框"><a href="#87、控制台输出白色方框" class="headerlink" title="87、控制台输出白色方框"></a>87、控制台输出白色方框</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(&#x27;█&#x27;)</span><br></pre></td></tr></table></figure>
<h2 id="88、xadmin后台编辑多对多字段"><a href="#88、xadmin后台编辑多对多字段" class="headerlink" title="88、xadmin后台编辑多对多字段"></a>88、xadmin后台编辑多对多字段</h2><p>在models.py定义了多对多字段，想要在编辑时可以灵活使用这个字段的话，可以按以下方法设置：<br>modes.py</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">class Book(models.Model):</span><br><span class="line">    title = models.CharField(verbose_name=&quot;书名&quot;, max_length=32)</span><br><span class="line">    second_title = models.CharField(verbose_name=&quot;副标题&quot;, max_length=32, blank=True, null=True)</span><br><span class="line">    author = models.CharField(verbose_name=&quot;作者&quot;, max_length=32)</span><br><span class="line">    translator = models.CharField(verbose_name=&quot;译者&quot;, max_length=32, blank=True, null=True)</span><br><span class="line">    intro = models.TextField(verbose_name=&quot;描述&quot;)</span><br><span class="line">    pic = models.FileField(verbose_name=&quot;封面图片&quot;, max_length=64, upload_to=&#x27;book_cover&#x27;, null=True, blank=True)</span><br><span class="line">    tags = models.ManyToManyField(Tags, verbose_name=&#x27;书籍标签&#x27;, blank=True)</span><br><span class="line">    prizes = models.ManyToManyField(Prizes, verbose_name=&#x27;获奖详情&#x27;, blank=True)</span><br><span class="line">    sump = models.IntegerField(verbose_name=&quot;收藏人数&quot;, default=0)</span><br><span class="line">    rate_num = models.IntegerField(verbose_name=&quot;评分人数&quot;, default=0)</span><br><span class="line">    num = models.IntegerField(verbose_name=&quot;浏览量&quot;, default=0)</span><br><span class="line">    published_time = models.DateField(blank=True, null=True, verbose_name=&#x27;出版时间&#x27;)</span><br><span class="line">    create_time = models.DateTimeField(auto_now_add=True, verbose_name=&#x27;创建时间&#x27;)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        db_table = &#x27;book&#x27;</span><br><span class="line">        verbose_name = &quot;图书&quot;</span><br><span class="line">        verbose_name_plural = &quot;图书&quot;</span><br><span class="line"></span><br><span class="line">    def __str__(self):</span><br><span class="line">        return self.title</span><br></pre></td></tr></table></figure>
<p>adminx.py</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"># 书籍管理</span><br><span class="line">class BookAdmin(object):</span><br><span class="line">    search_fields = [&#x27;title&#x27;, &#x27;author&#x27;, &#x27;intro&#x27;]  # 检索字段</span><br><span class="line">    list_display = [&#x27;id&#x27;, &#x27;show_pic&#x27;, &#x27;title&#x27;, &#x27;second_title&#x27;, &#x27;author&#x27;, &#x27;translator&#x27;, &#x27;published_time&#x27;, &#x27;intro&#x27;,</span><br><span class="line">                    &#x27;tags&#x27;, &#x27;prizes&#x27;, &#x27;num&#x27;, &#x27;sump&#x27;, &#x27;rate_num&#x27;]  # 要显示的字段</span><br><span class="line">    list_filter = [&#x27;published_time&#x27;, &#x27;tags&#x27;, &#x27;prizes&#x27;]  # 分组过滤的字段</span><br><span class="line">    ordering = (&#x27;id&#x27;,)  # 设置默认排序字段，负号表示降序排序</span><br><span class="line">    list_per_page = 30  # 默认每页显示多少条记录，默认是100条</span><br><span class="line">    model_icon = &#x27;fa fa-book&#x27;  # 左侧小图标</span><br><span class="line">    list_editable = [&#x27;title&#x27;, &#x27;author&#x27;, &#x27;intro&#x27;, &#x27;published_time&#x27;]  # 可编辑字段</span><br><span class="line">    style_fields = &#123;&#x27;tags&#x27;: &#x27;m2m_transfer&#x27;, &#x27;prizes&#x27;: &#x27;m2m_transfer&#x27;&#125;  # 控制字段的显示样式</span><br><span class="line">    filter_horizontal = (&#x27;tags&#x27;, &#x27;prizes&#x27;)  # 水平选择编辑多对多字段</span><br></pre></td></tr></table></figure>
<p>重点是设置style_fields 和filter_horizontal ，效果：</p>
<p><img src="https://img-blog.csdnimg.cn/20200621214122315.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h2 id="89、Django后台通过按钮显示TextField字段内容"><a href="#89、Django后台通过按钮显示TextField字段内容" class="headerlink" title="89、Django后台通过按钮显示TextField字段内容"></a>89、Django后台通过按钮显示TextField字段内容</h2><p>由于TextField字段类型内容可能很长，在后台显示时很占屏幕位置，可以通过按钮来控制显示，代码如下：<br>models.py中定义了一个TextField字段类型：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">class Prizes(models.Model):</span><br><span class="line">    name = models.CharField(max_length=32, verbose_name=&quot;奖项&quot;)</span><br><span class="line">    intro = models.TextField(blank=True, null=True, verbose_name=&#x27;简介&#x27;)</span><br><span class="line"></span><br><span class="line">    class Meta:</span><br><span class="line">        db_table = &#x27;prizes&#x27;</span><br><span class="line">        verbose_name = &quot;奖项&quot;</span><br><span class="line">        verbose_name_plural = &quot;奖项&quot;</span><br><span class="line"></span><br><span class="line">    def __str__(self):</span><br><span class="line">        return self.name</span><br></pre></td></tr></table></figure>
<p>这里使用xadmin作为后台管理框架，在adminx.py中代码如下：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">import xadmin</span><br><span class="line">from django.utils.safestring import mark_safe</span><br><span class="line">from xadmin import views</span><br><span class="line"></span><br><span class="line">from .models import *</span><br><span class="line"></span><br><span class="line"># 奖项管理</span><br><span class="line">class PrizesAdmin(object):</span><br><span class="line">    search_fields = [&#x27;name&#x27;]  # 检索字段</span><br><span class="line">    list_display = [&#x27;id&#x27;, &#x27;name&#x27;, &#x27;show_intro&#x27;]</span><br><span class="line">    list_filter = [&#x27;name&#x27;]</span><br><span class="line">    ordering = (&#x27;id&#x27;,)</span><br><span class="line"></span><br><span class="line">    def show_intro(self, obj):</span><br><span class="line">        # 显示简介</span><br><span class="line">        if not obj.intro:</span><br><span class="line">            return mark_safe(&#x27;&#x27;)</span><br><span class="line">        if len(obj.intro) &lt; 20:</span><br><span class="line">            return mark_safe(obj.intro)</span><br><span class="line"></span><br><span class="line">        short_id = f&#x27;&#123;obj._meta.db_table&#125;_short_text_&#123;obj.id&#125;&#x27;</span><br><span class="line">        short_text_len = len(obj.intro) // 4</span><br><span class="line">        short_text = obj.intro[:short_text_len] + &#x27;......&#x27;</span><br><span class="line">        detail_id = f&#x27;&#123;obj._meta.db_table&#125;_detail_text_&#123;obj.id&#125;&#x27;</span><br><span class="line">        detail_text = obj.intro</span><br><span class="line"></span><br><span class="line">        text = &quot;&quot;&quot;&lt;style type=&quot;text/css&quot;&gt;</span><br><span class="line">                        #%s,%s &#123;padding:10px;border:1px solid green;&#125; </span><br><span class="line">                  &lt;/style&gt;</span><br><span class="line">                    &lt;script type=&quot;text/javascript&quot;&gt;</span><br><span class="line"></span><br><span class="line">                    function openShutManager(oSourceObj,oTargetObj,shutAble,oOpenTip,oShutTip,oShortObj)&#123;</span><br><span class="line">                        var sourceObj = typeof oSourceObj == &quot;string&quot; ? document.getElementById(oSourceObj) : oSourceObj;</span><br><span class="line">                        var targetObj = typeof oTargetObj == &quot;string&quot; ? document.getElementById(oTargetObj) : oTargetObj;</span><br><span class="line">                        var shortObj = typeof oShortObj == &quot;string&quot; ? document.getElementById(oShortObj) : oShortObj;</span><br><span class="line">                        var openTip = oOpenTip || &quot;&quot;;</span><br><span class="line">                        var shutTip = oShutTip || &quot;&quot;;</span><br><span class="line">                        if(targetObj.style.display!=&quot;none&quot;)&#123;</span><br><span class="line">                           if(shutAble) return;</span><br><span class="line">                           targetObj.style.display=&quot;none&quot;;</span><br><span class="line">                           shortObj.style.display=&quot;block&quot;;</span><br><span class="line">                           if(openTip  &amp;&amp;  shutTip)&#123;</span><br><span class="line">                            sourceObj.innerHTML = shutTip; </span><br><span class="line">                           &#125;</span><br><span class="line">                        &#125; else &#123;</span><br><span class="line">                           targetObj.style.display=&quot;block&quot;;</span><br><span class="line">                           shortObj.style.display=&quot;none&quot;;</span><br><span class="line">                           if(openTip  &amp;&amp;  shutTip)&#123;</span><br><span class="line">                            sourceObj.innerHTML = openTip; </span><br><span class="line">                           &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &lt;/script&gt;</span><br><span class="line">                    &lt;p id=&quot;%s&quot;&gt;%s&lt;/p&gt;</span><br><span class="line">                    &lt;p&gt;&lt;a href=&quot;###&quot; οnclick=&quot;openShutManager(this,&#x27;%s&#x27;,false,&#x27;点击关闭&#x27;,&#x27;点击展开&#x27;,&#x27;%s&#x27;)&quot;&gt;点击展开&lt;/a&gt;&lt;/p&gt;</span><br><span class="line"></span><br><span class="line">                    &lt;p id=&quot;%s&quot; style=&quot;display:none&quot;&gt;</span><br><span class="line">                       %s</span><br><span class="line">                    &lt;/p&gt;</span><br><span class="line">                    &quot;&quot;&quot; % (short_id, detail_id, short_id, short_text, detail_id, short_id, detail_id, detail_text)</span><br><span class="line">        return mark_safe(text)</span><br><span class="line"></span><br><span class="line">    show_intro.short_description = &#x27;描述&#x27;</span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>：复制代码后需要做如下修改：</p>
<p><img src="https://img-blog.csdnimg.cn/20200815204246956.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<p>一开始效果</p>
<p><img src="https://img-blog.csdnimg.cn/20200621230038819.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>点击展开效果：<img src="https://img-blog.csdnimg.cn/20200621230120177.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h2 id="90、前端判断上传的文件是否为图片且限制大小为300kB"><a href="#90、前端判断上传的文件是否为图片且限制大小为300kB" class="headerlink" title="90、前端判断上传的文件是否为图片且限制大小为300kB"></a>90、前端判断上传的文件是否为图片且限制大小为300kB</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=&quot;en-us&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;body class=&quot;login&quot;&gt;</span><br><span class="line"></span><br><span class="line">&lt;input class=&quot;form-control input-lg&quot; id=&quot;id_icon&quot; name=&quot;icon&quot; type=&quot;file&quot; onchange=&quot;imgTypeSize(&#x27;id_icon&#x27;,300)&quot;</span><br><span class="line">&lt;script type=&quot;text/javascript&quot;&gt;</span><br><span class="line">    //判断是否为图片，若为图片，判断其大小是否大于0.3M</span><br><span class="line">    function imgTypeSize(FileId, maxsize) &#123;</span><br><span class="line">        /*获取图片内容对象*/</span><br><span class="line">        var imgFile = document.getElementById(FileId).files[0];</span><br><span class="line">        if (imgFile.name == &quot;&quot;) &#123;</span><br><span class="line">            alert(&quot;请上传头像哦&quot;);</span><br><span class="line">            return false;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            /*图片类型正则验证*/</span><br><span class="line">            var imgStr = /\.(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$/;</span><br><span class="line">            if (!imgStr.test(imgFile.name)) &#123;</span><br><span class="line">                alert(&quot;文件不是图片类型&quot;);</span><br><span class="line">                return false;</span><br><span class="line">            &#125; else &#123;</span><br><span class="line">                /*图片大小*/</span><br><span class="line">                var imagSize = imgFile.size;</span><br><span class="line">                if (imagSize &lt; (1024 * maxsize)) &#123;</span><br><span class="line">                    return true;</span><br><span class="line">                &#125; else &#123;</span><br><span class="line">                    alert(imgFile.name + &quot;大小不能超过&quot; + maxsize + &quot;kB&quot;);</span><br><span class="line">                    document.getElementById(FileId).value = &quot;&quot;</span><br><span class="line">                    return false;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;/script&gt;</span><br><span class="line"></span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
<h2 id="91、form表单提交前验证"><a href="#91、form表单提交前验证" class="headerlink" title="91、form表单提交前验证"></a>91、form表单提交前验证</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;login-form&quot; onsubmit=&quot;return toVaild()&quot;&gt;</span><br><span class="line">	&lt;input class=&quot;form-control&quot; id=&quot;id_account&quot; name=&quot;account&quot; type=&quot;text&quot; placeholder=&quot;账号&quot;/&gt;</span><br><span class="line">	&lt;input class=&quot;form-control&quot; id=&quot;id_password&quot; name=&quot;password&quot; type=&quot;password&quot; placeholder=&quot;密码&quot;&gt;</span><br><span class="line">&lt;form&gt;</span><br><span class="line">&lt;script type=&quot;text/javascript&quot;&gt;</span><br><span class="line">    document.getElementById(&#x27;id_username&#x27;).focus()</span><br><span class="line"></span><br><span class="line">    function toVaild() &#123;</span><br><span class="line">        var account = document.getElementById(&quot;id_account&quot;).value;</span><br><span class="line">        var password = document.getElementById(&quot;id_password&quot;).value;</span><br><span class="line">        if (account == &quot;&quot; || password == &quot;&quot;) &#123;</span><br><span class="line">            alert(&quot;请输入账号和密码&quot;);</span><br><span class="line">            return false;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<h2 id="92、有序字典"><a href="#92、有序字典" class="headerlink" title="92、有序字典"></a>92、有序字典</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">import collections</span><br><span class="line"></span><br><span class="line">d1 = collections.OrderedDict()  # 创建一个有序字典</span><br><span class="line">d1[&#x27;a&#x27;] = &#x27;A&#x27;</span><br><span class="line">d1[&#x27;b&#x27;] = &#x27;B&#x27;</span><br><span class="line">d1[&#x27;c&#x27;] = &#x27;C&#x27;</span><br><span class="line">d1[&#x27;d&#x27;] = &#x27;D&#x27;</span><br><span class="line">for k, v in d1.items():</span><br><span class="line">    print(k, v)</span><br></pre></td></tr></table></figure>
<h2 id="93、Python在Windows系统下实现TTS（文字转语音）"><a href="#93、Python在Windows系统下实现TTS（文字转语音）" class="headerlink" title="93、Python在Windows系统下实现TTS（文字转语音）"></a>93、Python在Windows系统下实现TTS（文字转语音）</h2><p>导入包：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">pip install pypiwin32</span><br><span class="line"></span><br><span class="line">import win32com.client</span><br><span class="line">spk = win32com.client.Dispatch(&quot;SAPI.SpVoice&quot;)</span><br><span class="line">spk.Speak(u&quot;my name is ldc,what is your name&quot;)</span><br><span class="line">spk.Speak(u&quot;大家好&quot;)</span><br></pre></td></tr></table></figure>
<h2 id="94、定义一个简单闹钟"><a href="#94、定义一个简单闹钟" class="headerlink" title="94、定义一个简单闹钟"></a>94、定义一个简单闹钟</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"># pip install pypiwin32 -i https://pypi.python.org/simple</span><br><span class="line">import win32com.client</span><br><span class="line">import time </span><br><span class="line">import winsound</span><br><span class="line"></span><br><span class="line">spk = win32com.client.Dispatch(&quot;SAPI.SpVoice&quot;)</span><br><span class="line"># 定义闹钟时间</span><br><span class="line">clocktime = [[19, 19], [11, 10], [12, 10], [18, 47]]  </span><br><span class="line">runinghour = 1  # 定义运行时间</span><br><span class="line">times = runinghour * 3600  # 次数</span><br><span class="line">print(times)</span><br><span class="line">for i in range(1, times):</span><br><span class="line">    time_now = [time.localtime(time.time()).tm_hour, time.localtime(time.time()).tm_min]</span><br><span class="line">    if time_now in clocktime:</span><br><span class="line">        print(time_now)</span><br><span class="line">        winsound.Beep(1000, 1000)</span><br><span class="line">        spk.Speak(u&quot;快去看下饭好了没有？&quot;)</span><br><span class="line">    time.sleep(60)  # 每分钟对比一次时间</span><br></pre></td></tr></table></figure>
<h2 id="95、根据年月获取当月天数"><a href="#95、根据年月获取当月天数" class="headerlink" title="95、根据年月获取当月天数"></a>95、根据年月获取当月天数</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">def get_month_days(year, month):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    根据年份，月份信息显示此月份天数</span><br><span class="line">    :param year: 年份：</span><br><span class="line">    :param month: 月份（1～12）：</span><br><span class="line">    :return: 当月天数</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    if month &gt;12 or month &lt;= 0:</span><br><span class="line">        return -1</span><br><span class="line">    if month == 2:</span><br><span class="line">        return 29 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else 28</span><br><span class="line"></span><br><span class="line">    if month in (4, 6, 9, 11):</span><br><span class="line">        return 30</span><br><span class="line">    else:</span><br><span class="line">        return 31</span><br><span class="line"></span><br><span class="line">a = &#x27;2020-04&#x27;.split(&#x27;-&#x27;)</span><br><span class="line">year = int(a[0])</span><br><span class="line">month = int(a[1])</span><br><span class="line">print(get_month_days(year,month))</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line">30</span><br></pre></td></tr></table></figure>
<h2 id="96、Django获取favicon-ico图标"><a href="#96、Django获取favicon-ico图标" class="headerlink" title="96、Django获取favicon.ico图标"></a>96、Django获取favicon.ico图标</h2><p>制作一个ico文件，使用PS或者某些在线生成ico的网站</p>
<p>将此文件命名为“favicon.ico”后放在static/下</p>
<p><img src="https://img-blog.csdnimg.cn/20200805104730363.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>在urls.py中定义路由：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">from django.urls import path</span><br><span class="line">from django.views.generic import RedirectView</span><br><span class="line"></span><br><span class="line">urlpatterns = [</span><br><span class="line">	# 访问favicon.ico 网站图标</span><br><span class="line">    path(&quot;favicon.ico&quot;,RedirectView.as_view(url=&#x27;static/favicon.ico&#x27;)),</span><br><span class="line"></span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h2 id="97、django-静态文件配置"><a href="#97、django-静态文件配置" class="headerlink" title="97、django 静态文件配置"></a>97、django 静态文件配置</h2><p>settings.py</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">STATIC_URL = &#x27;/static/&#x27;</span><br><span class="line"># 添加静态资源路由地址</span><br><span class="line">STATICFILES_DIRS = [</span><br><span class="line">    os.path.join(BASE_DIR, &#x27;static&#x27;),</span><br><span class="line"></span><br><span class="line">]</span><br><span class="line"># 使用python manage.py collectstatic收集静态文件时时使用STATIC_ROOT</span><br><span class="line"># STATIC_ROOT = os.path.join(BASE_DIR, &#x27;static&#x27;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>urls.py</p>
<pre><code>from django.urls import path, re_path
from django.views.generic import RedirectView
from django.views.static import serve

from &#39;你的项目名称&#39; import settings
urlpatterns = [

    path(&quot;favicon.ico&quot;,RedirectView.as_view(url=&#39;static/favicon.ico&#39;)),
    re_path(&#39;static/(?P&lt;path&gt;.*)$&#39;, serve, &#123;&#39;document_root&#39;: settings.STATICFILES_DIRS[0]&#125;),

]
</code></pre><h2 id="98、django-JsonResponse返回中文时显示unicode编码（-u67e5-u8be2）"><a href="#98、django-JsonResponse返回中文时显示unicode编码（-u67e5-u8be2）" class="headerlink" title="98、django JsonResponse返回中文时显示unicode编码（\u67e5\u8be2）"></a>98、django JsonResponse返回中文时显示unicode编码（\u67e5\u8be2）</h2><p>原因<br>这个unicode编码，是python3默认返回的编码。</p>
<p>解决方案<br>JsonResponse里面有个参数json_dumps_params，设置为json_dumps_params={‘ensure_ascii’:False}即可。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">data = &#123;&#x27;msg&#x27;: &#x27;创建成功&#x27;,&#125;</span><br><span class="line">return JsonResponse(data=data, json_dumps_params=&#123;&#x27;ensure_ascii&#x27;: False&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="99、获取当前时间月份的首日与最后一天"><a href="#99、获取当前时间月份的首日与最后一天" class="headerlink" title="99、获取当前时间月份的首日与最后一天"></a>99、获取当前时间月份的首日与最后一天</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">import calendar</span><br><span class="line"></span><br><span class="line">def get_month_start_and_end(date=datetime.datetime.now()):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    获取当前时间的月份首日与最后一天</span><br><span class="line">    :param date:</span><br><span class="line">    :return: (首日，最后一天)</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    year, month = str(date).split(&#x27;-&#x27;)[0], str(date).split(&#x27;-&#x27;)[1]</span><br><span class="line">    end = calendar.monthrange(int(year), int(month))[1]</span><br><span class="line">    return f&#x27;&#123;year&#125;-&#123;month&#125;-01&#x27;, f&#x27;&#123;year&#125;-&#123;month&#125;-&#123;end&#125;&#x27;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="100、xadmin后台批量操作"><a href="#100、xadmin后台批量操作" class="headerlink" title="100、xadmin后台批量操作"></a>100、xadmin后台批量操作</h2><p>adminx.py</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">import xadmin</span><br><span class="line">from django.db.models import  Sum</span><br><span class="line">from xadmin.plugins.actions import BaseActionView</span><br><span class="line"></span><br><span class="line">class MyCountFeeAction(BaseActionView):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">        用户余额统计</span><br><span class="line">        &quot;&quot;&quot;</span><br><span class="line">    action_name = &quot;countuserfee&quot;  #: 相当于这个 Action 的唯一标示, 尽量用比较针对性的名字</span><br><span class="line">    description = u&#x27;统计用户总余额&#x27;  #: 描述, 出现在 Action 菜单中, 可以使用 ``%(verbose_name_plural)s`` 代替 Model 的名字.</span><br><span class="line">    model_perm = &#x27;view&#x27;  # 权限</span><br><span class="line"></span><br><span class="line">    def do_action(self, queryset):</span><br><span class="line">        all_balance = MallUser.objects.all().aggregate(Sum(&#x27;balance&#x27;))</span><br><span class="line">        return HttpResponse(f&#x27;用户总余额&#123;all_balance&#125;&#x27;)</span><br><span class="line">class UserAdmin(object):</span><br><span class="line">    &quot;&quot;&quot;用户信息管理&quot;&quot;&quot;</span><br><span class="line">    list_display = [&#x27;username&#x27;, &#x27;balance&#x27;, &#x27;status&#x27;, &#x27;addtime&#x27;]</span><br><span class="line">    search_fields = [&#x27;username&#x27;, ]</span><br><span class="line">    list_filter = [&#x27;status&#x27;, &#x27;addtime&#x27;]</span><br><span class="line">    list_per_page = 30  # 默认每页数量</span><br><span class="line">    model_icon = &#x27;fa fa-user&#x27;</span><br><span class="line">    list_editable = [&#x27;status&#x27;]</span><br><span class="line">    ordering = [&#x27;-addtime&#x27;]</span><br><span class="line">    actions = [ MyCountFeeAction]  # 添加批量选择操作</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/20200812233442373.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<h2 id="101、xadmin过滤器外键显示特定值（比如只能过滤自己与超级管理员定义的数据）"><a href="#101、xadmin过滤器外键显示特定值（比如只能过滤自己与超级管理员定义的数据）" class="headerlink" title="101、xadmin过滤器外键显示特定值（比如只能过滤自己与超级管理员定义的数据）"></a>101、xadmin过滤器外键显示特定值（比如只能过滤自己与超级管理员定义的数据）</h2><p>首先，修改xadmin源码，修改xadmin/filters.py,在401行，做如下修改，</p>
<pre><code>把self.lookup_choices = field.get_choices(include_blank=False)
改为：

# 调用自定义的方法
if hasattr(model_admin, &#39;&#123;field&#125;_choices&#39;.format(field=field.name)):
        self.lookup_choices = getattr(model_admin, &#39;&#123;field&#125;_choices&#39;.format(field=field.name))(field, request,params, model,model_admin,field_path)
    else:
        self.lookup_choices = field.get_choices(include_blank=False)
</code></pre><p>如图:</p>
<p><img src="https://img-blog.csdnimg.cn/20200812234011656.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<p>然后，在adminx.py中定义过滤的方法：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">import xadmin</span><br><span class="line">from django.db.models import  Q, Sum</span><br><span class="line">from xadmin.plugins.actions import BaseActionView</span><br><span class="line"></span><br><span class="line">class MeasurePointAdmin(object):</span><br><span class="line">    # search_fields = [&#x27;user__name&#x27;, &#x27;user__account&#x27;]  # 检索字段</span><br><span class="line">    list_display = [&#x27;num&#x27;, &#x27;elevation&#x27;, &#x27;correct_num&#x27;, &#x27;cumulative_amount&#x27;]</span><br><span class="line">    list_filter = [&#x27;user&#x27;, &#x27;is_default&#x27;, &#x27;create_time&#x27;]  # 分组过滤的字段</span><br><span class="line">    list_editable = [&#x27;num&#x27;, &#x27;elevation&#x27;, &#x27;correct_num&#x27;, &#x27;cumulative_amount&#x27;]</span><br><span class="line">    ordering = (&#x27;id&#x27;,)  # 设置默认排序字段，负号表示降序排序</span><br><span class="line">    list_per_page = 30  # 默认每页显示多少条记录，默认是100条</span><br><span class="line">    model_icon = &#x27;fa fa-users&#x27;  # 左侧小图标</span><br><span class="line">    readonly_fields = [&#x27;user&#x27;, &#x27;is_default&#x27;]</span><br><span class="line">    import_excel = True</span><br><span class="line">    actions = [MyCountFeeAction]</span><br><span class="line">	</span><br><span class="line">	# 定义的函数名必须是  字段名_choices</span><br><span class="line">    def user_choices(self, field, request, params, model, model_admin, field_path):</span><br><span class="line">        # 超级用户不做控制</span><br><span class="line">        if self.request.user.is_superuser:</span><br><span class="line">            return field.get_choices(include_blank=False)</span><br><span class="line"></span><br><span class="line">        # 过滤器只显示自己与超级管理员</span><br><span class="line">        user_lst = field.related_model._default_manager.filter(Q(id=self.request.user.id) | Q(is_superuser=True))</span><br><span class="line">        # 返回格式 [(&#x27;pk&#x27;,&#x27;标题&#x27;),]</span><br><span class="line">        return [(user.id, user.username) for user in user_lst]</span><br></pre></td></tr></table></figure>
<p>效果:</p>
<p><img src="https://img-blog.csdnimg.cn/2020081223440761.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xtX2lzX2Rj,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述"></p>
<h2 id="102、Django中的CSRF局部禁用"><a href="#102、Django中的CSRF局部禁用" class="headerlink" title="102、Django中的CSRF局部禁用"></a>102、Django中的CSRF局部禁用</h2><p>为了避免没有csrf token而产生的403的forbidden错误，可以使用csrf_exempt装饰器来处理POST、GET请求的View, 这种方式是CSRF局部禁用。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">from django.utils.decorators import method_decorator</span><br><span class="line">from django.views.decorators.csrf import csrf_exempt</span><br><span class="line"></span><br><span class="line">@method_decorator(csrf_exempt)</span><br><span class="line">def update_data(request):</span><br><span class="line">    if request.method == &#x27;POST&#x27;:</span><br><span class="line">    	pass</span><br><span class="line">	elif request.method == &#x27;GET&#x27;:</span><br><span class="line">    	pass</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="103、使用python命令创建django项目"><a href="#103、使用python命令创建django项目" class="headerlink" title="103、使用python命令创建django项目"></a>103、使用python命令创建django项目</h2><p>创建django项目<br>方式一：直接打开pycharm创建，选择file-&gt;new project-&gt;django<br>方式二：通过命令创建<br>先创建虚拟环境：python -m venv django_venv<br>然后导入django库：pip install django -i <a target="_blank" rel="noopener" href="https://pypi.tuna.tsinghua.edu.cn/simple">https://pypi.tuna.tsinghua.edu.cn/simple</a><br>最后使用命令创建：django-admin startproject my_django_project</p>
<h2 id="104、-二维列表、二维数组行求和与列求和"><a href="#104、-二维列表、二维数组行求和与列求和" class="headerlink" title="104、 二维列表、二维数组行求和与列求和"></a>104、 二维列表、二维数组行求和与列求和</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">import pandas as pd</span><br><span class="line">from functools import reduce</span><br><span class="line"></span><br><span class="line"># 二维数组，对列和行求和</span><br><span class="line">a = [</span><br><span class="line">    [&#x27;&#x27;, 0, &#x27;&#x27;, 1, 4.1],</span><br><span class="line">    [&#x27;&#x27;, 0, &#x27;&#x27;, &#x27;&#x27;, 4],</span><br><span class="line">    [&#x27;123&#x27;, 0, &#x27;&#x27;, 3, 4, 6,7],</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def aa(x, y):</span><br><span class="line">    x = 0 if isinstance(x, str) else x</span><br><span class="line">    y = 0 if isinstance(y, str) else y</span><br><span class="line">    return x + y</span><br><span class="line"></span><br><span class="line"># 对行求和</span><br><span class="line">row_sum = [reduce(aa, i) for i in a]</span><br><span class="line"># 对列求和，只能处理相同长度的子元素</span><br><span class="line">column_sum_1 = [reduce(aa, i) for i in zip(*a)]</span><br><span class="line"># 对列求和，可以处理不同长度的子元素</span><br><span class="line">column_sum_2 = list(dict(pd.DataFrame(a).fillna(0).apply(lambda x: &#x27;&#x27; if any(isinstance(d, str) for d in x) else round(x.sum(), 2))).values())</span><br><span class="line">print(&#x27;行求和：&#123;&#125;\r\n列求和(相同长度)：&#123;&#125;\r\n列求和：&#123;&#125;&#x27;.format(row_sum, column_sum_1, column_sum_2))</span><br></pre></td></tr></table></figure>
<p>输出：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">行求和：[5.1, 4, 20]</span><br><span class="line">列求和(相同长度)：[0, 0, 0, 4, 12.1]</span><br><span class="line">列求和：[&#x27;&#x27;, 0, &#x27;&#x27;, &#x27;&#x27;, 12.1, 6.0, 7.0]</span><br></pre></td></tr></table></figure>
<h2 id="105、获取时间字符串的月份数"><a href="#105、获取时间字符串的月份数" class="headerlink" title="105、获取时间字符串的月份数"></a>105、获取时间字符串的月份数</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">from datetime import datetime</span><br><span class="line">    </span><br><span class="line">def get_months(start_month_str, end_month_str):</span><br><span class="line">    &#x27;&#x27;&#x27;</span><br><span class="line">    # 获取时间字符串中的月份数</span><br><span class="line">    :param start_month_str: 开始字符串</span><br><span class="line">    :param end_month_str: 结束字符串</span><br><span class="line">    :return: 月份数</span><br><span class="line">    &#x27;&#x27;&#x27;</span><br><span class="line"></span><br><span class="line">    end_month_date = datetime.strptime(end_month_str, &#x27;%Y-%m&#x27;)</span><br><span class="line">    start_month_date = datetime.strptime(start_month_str, &#x27;%Y-%m&#x27;)</span><br><span class="line">    end_year, end_month = end_month_date.year, end_month_date.month</span><br><span class="line">    start_year, start_month = start_month_date.year, start_month_date.month</span><br><span class="line"></span><br><span class="line">    return (end_year - start_year) *12 + (end_month - start_month) + 1</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">end_month_str = &#x27;2021-02&#x27;</span><br><span class="line">start_month_str = &#x27;2020-07&#x27;</span><br><span class="line">print(get_months(start_month_str, end_month_str))</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"></span><br><span class="line">8</span><br></pre></td></tr></table></figure>
<h2 id="106、字符串不足补零"><a href="#106、字符串不足补零" class="headerlink" title="106、字符串不足补零"></a>106、字符串不足补零</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">print(&#x27;hello world&#x27;.zfill(15)) # 补0</span><br><span class="line">print(&#x27;hello world&#x27;.rjust(15)) # 右对齐，补空格</span><br><span class="line">print(&#x27;hello world&#x27;.ljust(15)) # 左对齐，补空格</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"></span><br><span class="line">0000hello world</span><br><span class="line">    hello world</span><br><span class="line">hello world  </span><br></pre></td></tr></table></figure>
<h2 id="107、时间戳转字符串日期"><a href="#107、时间戳转字符串日期" class="headerlink" title="107、时间戳转字符串日期"></a>107、时间戳转字符串日期</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line">t1 = time.time()</span><br><span class="line"><span class="built_in">print</span>(t1)</span><br><span class="line"><span class="built_in">print</span>(time.strftime(<span class="string">&quot;%Y-%m-%d %H:%M:%S&quot;</span>, time.localtime(t1)))</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"><span class="number">1621741567.082192</span></span><br><span class="line"><span class="number">2021</span>-05-<span class="number">23</span> <span class="number">11</span>:<span class="number">46</span>:07</span><br></pre></td></tr></table></figure>
<p>使用函数：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"># !/usr/bin/python</span><br><span class="line"># -*- coding: utf-8 -*-</span><br><span class="line"></span><br><span class="line">&quot;&quot;&quot;</span><br><span class="line">@contact: 微信 1257309054</span><br><span class="line">@file: 时间戳转日期.py</span><br><span class="line">@time: 2022/6/27 17:16</span><br><span class="line">@author: LDC</span><br><span class="line">&quot;&quot;&quot;</span><br><span class="line">import time</span><br><span class="line">import datetime</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"># 正确10位长度的时间戳可精确到秒，11-14位长度则是包含了毫秒</span><br><span class="line">def int_to_datetime(intValue):</span><br><span class="line">    if len(str(intValue)) == 10:</span><br><span class="line">        # 精确到秒</span><br><span class="line">        timeValue = time.localtime(intValue)</span><br><span class="line">        tempDate = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, timeValue)</span><br><span class="line">        datetimeValue = datetime.datetime.strptime(tempDate, &quot;%Y-%m-%d %H:%M:%S&quot;)</span><br><span class="line">    elif 10 &lt; len(str(intValue)) and len(str(intValue)) &lt; 15:</span><br><span class="line">        # 精确到毫秒</span><br><span class="line">        k = len(str(intValue)) - 10</span><br><span class="line">        timetamp = datetime.datetime.fromtimestamp(intValue / (1 * 10 ** k))</span><br><span class="line">        datetimeValue = timetamp.strftime(&quot;%Y-%m-%d %H:%M:%S.%f&quot;)</span><br><span class="line">    else:</span><br><span class="line">        return -1</span><br><span class="line">    return datetimeValue</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">time1 = 1656321420</span><br><span class="line">time2 = 1656321086560</span><br><span class="line">print(int_to_datetime(time1))</span><br><span class="line">print(int_to_datetime(time2))</span><br><span class="line"></span><br><span class="line">&#x27;&#x27;&#x27;</span><br><span class="line">输出：</span><br><span class="line">2022-06-27 17:17:00</span><br><span class="line">2022-06-27 17:11:26.560000</span><br><span class="line">&#x27;&#x27;&#x27;</span><br></pre></td></tr></table></figure>
<h2 id="108、列表转字符串"><a href="#108、列表转字符串" class="headerlink" title="108、列表转字符串"></a>108、列表转字符串</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;,&#x27;</span>.join(<span class="built_in">map</span>(<span class="built_in">str</span>,a)))</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line">`<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>`</span><br></pre></td></tr></table></figure>
<h2 id="109、pip国内镜像源"><a href="#109、pip国内镜像源" class="headerlink" title="109、pip国内镜像源"></a>109、pip国内镜像源</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">pip install 库名 -i https://pypi.tuna.tsinghua.edu.cn/simple</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">清华：-i https://pypi.tuna.tsinghua.edu.cn/simple</span><br><span class="line"></span><br><span class="line">阿里云：-i http://mirrors.aliyun.com/pypi/simple/</span><br><span class="line"></span><br><span class="line">中国科技大学 -i https://pypi.mirrors.ustc.edu.cn/simple/</span><br><span class="line"></span><br><span class="line">华中理工大学：-i http://pypi.hustunique.com/</span><br><span class="line"></span><br><span class="line">山东理工大学：-i http://pypi.sdutlinux.org/ </span><br><span class="line"></span><br><span class="line">豆瓣：-i http://pypi.douban.com/simple/</span><br></pre></td></tr></table></figure>
<h2 id="110、python把时间字符串转换成刚刚、1天前、3个月前、1年前"><a href="#110、python把时间字符串转换成刚刚、1天前、3个月前、1年前" class="headerlink" title="110、python把时间字符串转换成刚刚、1天前、3个月前、1年前"></a>110、python把时间字符串转换成刚刚、1天前、3个月前、1年前</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">import datetime</span><br><span class="line">import time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">def date_interval(date_str):</span><br><span class="line">    &#x27;&#x27;&#x27;</span><br><span class="line">    获取时间间隔</span><br><span class="line">    1分钟前，2分钟前，10分钟前，1小时前，2小时前，1天前，2天前，3天前，1个月前，3个月前，1年前，3年前</span><br><span class="line">    :param date_str: 时间字符串</span><br><span class="line">    :return: 字符串</span><br><span class="line">    &#x27;&#x27;&#x27;</span><br><span class="line">    date_str = time.strptime(date_str, &#x27;%Y-%m-%d %H:%M:%S&#x27;)</span><br><span class="line">    # 将时间元组转换为时间戳</span><br><span class="line">    t = time.mktime(date_str)</span><br><span class="line"></span><br><span class="line">    # 当前时间</span><br><span class="line">    seconds = time.time() - t</span><br><span class="line"></span><br><span class="line">    years = int(seconds // (60 * 60 * 24 * 365))</span><br><span class="line">    if years:</span><br><span class="line">        return &#x27;&#123;&#125;年前&#x27;.format(years)</span><br><span class="line">    months = int(seconds // (60 * 60 * 24 * 30))</span><br><span class="line">    if months:</span><br><span class="line">        return &#x27;&#123;&#125;月前&#x27;.format(months)</span><br><span class="line">    days = int(seconds // (60 * 60 * 24))</span><br><span class="line">    if days:</span><br><span class="line">        return &#x27;&#123;&#125;天前&#x27;.format(days)</span><br><span class="line">    hours = int(seconds // (60 * 60))</span><br><span class="line">    if hours:</span><br><span class="line">        return &#x27;&#123;&#125;小时前&#x27;.format(hours)</span><br><span class="line">    minutes = int(seconds // (60))</span><br><span class="line">    if minutes:</span><br><span class="line">        return &#x27;&#123;&#125;分钟前&#x27;.format(minutes)</span><br><span class="line">    return &#x27;刚刚&#x27;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == &#x27;__main__&#x27;:</span><br><span class="line">    date1 = &#x27;2019-07-10 15:27:51&#x27;</span><br><span class="line">    date2 = &#x27;2021-07-10 15:27:51&#x27;</span><br><span class="line">    date3 = &#x27;2021-08-10 15:27:51&#x27;</span><br><span class="line">    date4 = &#x27;2021-08-12 11:01:51&#x27;</span><br><span class="line">    date5 = datetime.datetime.now() + datetime.timedelta(seconds=-3)</span><br><span class="line">    date5 = date5.strftime(&#x27;%Y-%m-%d %H:%M:%S&#x27;)</span><br><span class="line">    print(date_interval(date1))</span><br><span class="line">    print(date_interval(date2))</span><br><span class="line">    print(date_interval(date3))</span><br><span class="line">    print(date_interval(date4))</span><br><span class="line">    print(date_interval(date5))</span><br></pre></td></tr></table></figure>
<h2 id="111、Django使用ORM执行sql语句"><a href="#111、Django使用ORM执行sql语句" class="headerlink" title="111、Django使用ORM执行sql语句"></a>111、Django使用ORM执行sql语句</h2><p>通过raw函数执行</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">alarm_datas = gsm.AlertMessage.objects.raw(&#x27;&#x27;&#x27;</span><br><span class="line">                select am_all.id, am_all.sensor_number</span><br><span class="line">                    FROM alert_message as am_all</span><br><span class="line">                    where </span><br><span class="line">                        am_all.real=&#x27;1&#x27;</span><br><span class="line">                        and am_all.is_show=1</span><br><span class="line">                    order by am_all.alarm_level desc</span><br><span class="line">                &#x27;&#x27;&#x27;)</span><br><span class="line">for ad in alarm_datas:</span><br><span class="line">	print(ad.sensor_number)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="112、Django通过变量字段获取数据"><a href="#112、Django通过变量字段获取数据" class="headerlink" title="112、Django通过变量字段获取数据"></a>112、Django通过变量字段获取数据</h2><p>通过方法<code>__getattribute__</code>来获取</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">alert_messages = AlertMessage.objects.filter(real=0)</span><br><span class="line">am_fields = [&#x27;id&#x27;, &#x27;real&#x27;, &#x27;name&#x27;]</span><br><span class="line">for am in alert_messages:</span><br><span class="line">	for i in range(len(am_fields)):</span><br><span class="line">		print(am_fields[i], am.__getattribute__(am_fields[i]))</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="113、Django查询变量字段、更新变量字段"><a href="#113、Django查询变量字段、更新变量字段" class="headerlink" title="113、Django查询变量字段、更新变量字段"></a>113、Django查询变量字段、更新变量字段</h2><p>通过<code>**&#123;变量名:值&#125;</code>来操作</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">field = &#x27;device_code&#x27;</span><br><span class="line"># 查询</span><br><span class="line">device_infos = gsm.DeviceInfo.objects.filter(**&#123;field: &#x27;012&#x27;&#125;)</span><br><span class="line"># 更新</span><br><span class="line">gsm.DeviceInfo.objects.filter(device_code=&#x27;012&#x27;).update(**&#123;field : &#x27;0&#x27;&#125;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="114、vue生成dist文件"><a href="#114、vue生成dist文件" class="headerlink" title="114、vue生成dist文件"></a>114、vue生成dist文件</h2><p>安装npm，到<a target="_blank" rel="noopener" href="https://nodejs.org/en/download/">官网下载</a><br>在vue项目下打开cmd，输入命令</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run build</span><br></pre></td></tr></table></figure>
<p>如果报错，npm ERR! missing script: build，<br>则使用以下命令</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm run build:prod</span><br></pre></td></tr></table></figure>
<p>具体看package.json</p>
<p><img src="https://img-blog.csdnimg.cn/eff15dcd4c1d499f9641d0ac25c09bc0.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5Lic5pyo5pyI,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h2 id="115、python获取电脑磁盘、CPU、内存使用情况"><a href="#115、python获取电脑磁盘、CPU、内存使用情况" class="headerlink" title="115、python获取电脑磁盘、CPU、内存使用情况"></a>115、python获取电脑磁盘、CPU、内存使用情况</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">import psutil</span><br><span class="line">#  pip install psutil</span><br><span class="line"></span><br><span class="line"># 获取本机磁盘使用率和剩余空间G信息</span><br><span class="line">def get_disk_info():</span><br><span class="line">    # 循环磁盘分区</span><br><span class="line">    content = &quot;&quot;</span><br><span class="line">    for disk in psutil.disk_partitions():</span><br><span class="line">        # 读写方式 光盘 or 有效磁盘类型</span><br><span class="line">        if &#x27;cdrom&#x27; in disk.opts or disk.fstype == &#x27;&#x27;:</span><br><span class="line">            continue</span><br><span class="line">        disk_name_arr = disk.device.split(&#x27;:&#x27;)</span><br><span class="line">        disk_name = disk_name_arr[0]</span><br><span class="line">        disk_info = psutil.disk_usage(disk.device)</span><br><span class="line">        # 磁盘剩余空间，单位G</span><br><span class="line">        free_disk_size = disk_info.free//1024//1024//1024</span><br><span class="line">        # 当前磁盘使用率和剩余空间G信息</span><br><span class="line">        info = &quot;&#123;&#125;盘使用率：&#123;&#125;%%， 剩余空间：&#123;&#125;G &quot;.format(disk_name, str(disk_info.percent), free_disk_size)</span><br><span class="line">        # 拼接多个磁盘的信息</span><br><span class="line">        content = content + info</span><br><span class="line">    print(content)</span><br><span class="line"></span><br><span class="line"># cpu信息</span><br><span class="line">def get_cpu_info():</span><br><span class="line">    cpu_percent = psutil.cpu_percent(interval=1)</span><br><span class="line">    cpu_info = &quot;CPU使用率：%i%%&quot; % cpu_percent</span><br><span class="line">    print(cpu_info)</span><br><span class="line"></span><br><span class="line"># 内存信息</span><br><span class="line">def get_memory_info():</span><br><span class="line">    virtual_memory = psutil.virtual_memory()</span><br><span class="line">    used_memory = virtual_memory.used/1024/1024/1024</span><br><span class="line">    free_memory = virtual_memory.free/1024/1024/1024</span><br><span class="line">    memory_percent = virtual_memory.percent</span><br><span class="line">    memory_info = &quot;内存使用：%0.2fG，使用率%0.1f%%，剩余内存：%0.2fG&quot; % (used_memory, memory_percent, free_memory)</span><br><span class="line">    print(memory_info)</span><br><span class="line"></span><br><span class="line">if __name__ == &#x27;__main__&#x27;:</span><br><span class="line">    get_disk_info()</span><br><span class="line">    get_cpu_info()</span><br><span class="line">    get_memory_info()</span><br></pre></td></tr></table></figure>
<h2 id="116、max比较字典列表"><a href="#116、max比较字典列表" class="headerlink" title="116、max比较字典列表"></a>116、max比较字典列表</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&#x27;&#x27;&#x27;</span><br><span class="line">使用max函数比较字典列表对象</span><br><span class="line">需求：找出用户组最大id的字典</span><br><span class="line">&#x27;&#x27;&#x27;</span><br><span class="line">def com_id(f):</span><br><span class="line">    &#x27;&#x27;&#x27;</span><br><span class="line">    比较用用户ids列表</span><br><span class="line">    :param f: 字典</span><br><span class="line">    :return: 返回最大的用户id</span><br><span class="line">    &#x27;&#x27;&#x27;</span><br><span class="line">    return max(f[&#x27;user_ids&#x27;])</span><br><span class="line"></span><br><span class="line">friends_added = [</span><br><span class="line">        &#123;&#x27;user_ids&#x27;: [1, 2], &#x27;create_at&#x27;: &#x27;2020-01-01&#x27;&#125;,</span><br><span class="line">        &#123;&#x27;user_ids&#x27;: [3, 6], &#x27;create_at&#x27;: &#x27;2020-01-02&#x27;&#125;,</span><br><span class="line">        &#123;&#x27;user_ids&#x27;: [2, 1], &#x27;create_at&#x27;: &#x27;2020-02-02&#x27;&#125;,</span><br><span class="line">        &#123;&#x27;user_ids&#x27;: [4, 1], &#x27;create_at&#x27;: &#x27;2020-02-02&#x27;&#125;,</span><br><span class="line">    ]</span><br><span class="line"># max函数中使用key参数，指定自定义函数来比较</span><br><span class="line">item = max(friends_added, key=com_id)</span><br><span class="line">item_1 = max(friends_added, key=lambda f: max(f[&#x27;user_ids&#x27;])) # com_id可以转成lambda函数</span><br><span class="line">print(item)</span><br><span class="line">print(item_1)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"></span><br><span class="line">&#123;&#x27;user_ids&#x27;: [3, 6], &#x27;create_at&#x27;: &#x27;2020-01-02&#x27;&#125;</span><br><span class="line">&#123;&#x27;user_ids&#x27;: [3, 6], &#x27;create_at&#x27;: &#x27;2020-01-02&#x27;&#125;</span><br></pre></td></tr></table></figure>
<h2 id="117、django查询表的具体字段"><a href="#117、django查询表的具体字段" class="headerlink" title="117、django查询表的具体字段"></a>117、django查询表的具体字段</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ExampleModel.objects.values(&#x27;id&#x27;, &#x27;username&#x27;)</span><br><span class="line"># 返回的结果是QuerySet， 里面的元素是dict格式</span><br><span class="line"># [&#123;&#x27;id&#x27;:1, &#x27;username&#x27;: &#x27;Austin&#x27;&#125;, &#123;&#x27;id&#x27;:2, &#x27;username&#x27;:&#x27;Sam&#x27;&#125;, ...]</span><br><span class="line"># 相当于SQL中的SELECT id, username FROM ......, </span><br><span class="line"># 只返回对应属性（字段）的值</span><br></pre></td></tr></table></figure>
<h2 id="118、MySQL查询数据添加自增序号"><a href="#118、MySQL查询数据添加自增序号" class="headerlink" title="118、MySQL查询数据添加自增序号"></a>118、MySQL查询数据添加自增序号</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">set @i = 0;</span><br><span class="line">select waveform, &#x27;沿桥风吹&#x27; as label ,(@i:=@i+1) xh from alert_message where is_real=1 and node_id=138</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="119、MySQL-时间截取年月日"><a href="#119、MySQL-时间截取年月日" class="headerlink" title="119、MySQL 时间截取年月日"></a>119、MySQL 时间截取年月日</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">select id,</span><br><span class="line">			addtime, </span><br><span class="line">			date_format(addtime,&#x27;%Y-%m-%d&#x27;) as short_time </span><br><span class="line">	from alert_message </span><br><span class="line">	where date_format(addtime,&#x27;%Y-%m-%d&#x27;) = &#x27;2022-03-13&#x27;</span><br></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/37cec67fa1e1458f9aba1b745600a493.png" alt="在这里插入图片描述"></p>
<h2 id="120、-列表取奇数下标值"><a href="#120、-列表取奇数下标值" class="headerlink" title="120、 列表取奇数下标值"></a>120、 列表取奇数下标值</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">list1=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;奇数下标值&#x27;</span>,list1[<span class="number">1</span>::<span class="number">2</span>])</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"></span><br><span class="line">奇数下标值 [<span class="string">&#x27;b&#x27;</span>, <span class="string">&#x27;d&#x27;</span>]</span><br></pre></td></tr></table></figure>
<h2 id="121、列表取偶数下标值"><a href="#121、列表取偶数下标值" class="headerlink" title="121、列表取偶数下标值"></a>121、列表取偶数下标值</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">list1=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>]</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;偶数下标值&#x27;</span>,list1[::<span class="number">2</span>])</span><br><span class="line">偶数下标值 [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;c&#x27;</span>, <span class="string">&#x27;e&#x27;</span>]</span><br></pre></td></tr></table></figure>
<h2 id="122、列表相同元素分类、分组"><a href="#122、列表相同元素分类、分组" class="headerlink" title="122、列表相同元素分类、分组"></a>122、列表相同元素分类、分组</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> itertools</span><br><span class="line"></span><br><span class="line">data = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">1</span>, <span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>]</span><br><span class="line">a =[<span class="built_in">list</span>(group) <span class="keyword">for</span> key, group <span class="keyword">in</span> itertools.groupby(data)]</span><br><span class="line"><span class="built_in">print</span>(a)</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"></span><br><span class="line">[[<span class="number">1</span>], [<span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>, <span class="number">2</span>], [<span class="number">3</span>, <span class="number">3</span>, <span class="number">3</span>], [<span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>, <span class="number">4</span>], [<span class="number">1</span>], [<span class="string">&#x27;a&#x27;</span>, <span class="string">&#x27;a&#x27;</span>]]</span><br></pre></td></tr></table></figure>
<h2 id="123、ubuntu切换用户"><a href="#123、ubuntu切换用户" class="headerlink" title="123、ubuntu切换用户"></a>123、ubuntu切换用户</h2><p>1）从user用户切换到root用户<br>sudo su<br>2）从root用户切回user用户<br>su user (user是你自己安装时候的用户名)，或是直接输入exit，也可Ctrl+D组合键推出</p>
<h2 id="124、numpy二维数组获取某一列"><a href="#124、numpy二维数组获取某一列" class="headerlink" title="124、numpy二维数组获取某一列"></a>124、numpy二维数组获取某一列</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">import numpy as np</span><br><span class="line"></span><br><span class="line">a = [</span><br><span class="line">    [1, 2, 3, 4, 5, 6],</span><br><span class="line">    [7, 8, 9, 10, 11, 12],</span><br><span class="line">    [13.2, 14.8, 15.9, 16.10, 16.11, 17.12],</span><br><span class="line">]</span><br><span class="line"></span><br><span class="line">a_np = np.array(a)  # 把二维列表转成numpy数组</span><br><span class="line">print(&#x27;第一行&#x27;, a_np[0].tolist())  # 获取第一行</span><br><span class="line">print(&#x27;第一列&#x27;, a_np[:, 0].tolist())  # 获取第一列</span><br><span class="line"></span><br><span class="line">输出：</span><br><span class="line"></span><br><span class="line">第一行 [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]</span><br><span class="line">第一列 [1.0, 7.0, 13.2]</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io">kiloGrand</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io/2021/08/19/python-useful-function/">https://kilogrand.gitee.io/2021/08/19/python-useful-function/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kilogrand.gitee.io" target="_blank">kiloGrand</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/python/">python</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/08/19/EI-clock-conclusion/"><img class="prev-cover" src="/img/lab.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">自制电子时钟</div></div></a></div><div class="next-post pull-right"><a href="/2022/05/11/blog-hello-world/"><img class="next-cover" src="/img/background.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Hello World</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/profile.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">kiloGrand</div><div class="author-info__description">coder && data-science researcher</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kiloGrand/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E7%94%9F%E6%88%906%E4%BD%8D%E6%95%B0%E5%AD%97%E9%9A%8F%E6%9C%BA%E9%AA%8C%E8%AF%81%E7%A0%81"><span class="toc-number">1.</span> <span class="toc-text">1. 生成6位数字随机验证码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-md5%E5%8A%A0%E5%AF%86"><span class="toc-number">2.</span> <span class="toc-text">2.md5加密</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E7%94%9F%E6%88%90%E5%94%AF%E4%B8%80token"><span class="toc-number">3.</span> <span class="toc-text">3. 生成唯一token</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E5%8F%91%E9%80%81%E6%89%8B%E6%9C%BA%E9%AA%8C%E8%AF%81%E7%A0%81"><span class="toc-number">4.</span> <span class="toc-text">4、发送手机验证码</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0%E9%80%BB%E8%BE%91"><span class="toc-number">4.1.</span> <span class="toc-text">实现逻辑</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E7%94%9F%E6%88%90%E4%BA%8C%E7%BB%B4%E7%A0%81"><span class="toc-number">5.</span> <span class="toc-text">5、生成二维码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E5%BE%AE%E4%BF%A1%E7%BE%A4%E5%8F%91"><span class="toc-number">6.</span> <span class="toc-text">6.微信群发</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E5%BE%AE%E4%BF%A1%E8%87%AA%E5%8A%A8%E5%9B%9E%E5%A4%8D"><span class="toc-number">7.</span> <span class="toc-text">7、微信自动回复</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81%E6%8F%90%E5%8F%96Django%E4%B8%ADmodel%E4%B8%AD%E7%9A%84%E5%AD%97%E6%AE%B5%E5%90%8D%E5%8F%98%E6%88%90%E5%AD%97%E5%85%B8%E3%80%81%E5%88%97%E8%A1%A8"><span class="toc-number">8.</span> <span class="toc-text">8、提取Django中model中的字段名变成字典、列表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E7%BB%99%E8%A1%A8%E5%88%9B%E5%BB%BA%E6%95%B0%E6%8D%AE"><span class="toc-number">9.</span> <span class="toc-text">9、数据库中给表创建数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-%E6%8D%95%E6%8D%89%E5%BC%82%E5%B8%B8"><span class="toc-number">10.</span> <span class="toc-text">10.捕捉异常</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11%E3%80%81%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4"><span class="toc-number">11.</span> <span class="toc-text">11、获取当前时间</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12%E3%80%81%E8%AE%A2%E5%8D%95%E7%BC%96%E5%8F%B7"><span class="toc-number">12.</span> <span class="toc-text">12、订单编号</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13%E3%80%81mysql%E8%87%AA%E5%8A%A8%E5%A1%AB%E5%86%99%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4"><span class="toc-number">13.</span> <span class="toc-text">13、mysql自动填写当前时间</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14%E3%80%81drf%E5%8A%A8%E6%80%81%E8%BF%87%E6%BB%A4%E6%9F%A5%E8%AF%A2"><span class="toc-number">14.</span> <span class="toc-text">14、drf动态过滤查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15%E3%80%81linux%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8Cpython%E7%A8%8B%E5%BA%8F"><span class="toc-number">15.</span> <span class="toc-text">15、linux后台运行python程序</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16%E3%80%81%E8%BF%BD%E5%8A%A0%E5%A4%96%E9%94%AE"><span class="toc-number">16.</span> <span class="toc-text">16、追加外键</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#17%E3%80%81%E5%86%99-%E8%AF%BBCSV%E6%96%87%E4%BB%B6%EF%BC%8C%E6%9F%A5%E7%9C%8B%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%EF%BC%8C%E8%8B%A5%E5%AD%98%E5%9C%A8%E5%B0%B1%E4%BB%8Ecsv%E4%B8%AD%E5%88%A0%E9%99%A4"><span class="toc-number">17.</span> <span class="toc-text">17、写&#x2F;读CSV文件，查看是否存在，若存在就从csv中删除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#18%E3%80%81django%E4%B8%AD%E4%BB%8Erequest%E8%8E%B7%E5%8F%96%E8%AE%BF%E9%97%AE%E8%B7%AF%E5%BE%84"><span class="toc-number">18.</span> <span class="toc-text">18、django中从request获取访问路径</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#19%E3%80%81Django%E6%94%B6%E9%9B%86%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6"><span class="toc-number">19.</span> <span class="toc-text">19、Django收集静态文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#20%E3%80%81xadmin%E6%8F%92%E4%BB%B6"><span class="toc-number">20.</span> <span class="toc-text">20、xadmin插件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#21%E3%80%81uwsgi%E8%87%AA%E5%8A%A8%E9%87%8D%E5%90%AF"><span class="toc-number">21.</span> <span class="toc-text">21、uwsgi自动重启</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#22%E3%80%81%E8%B0%B7%E6%AD%8C%E6%B5%8F%E8%A7%88%E5%99%A8%E5%BF%AB%E6%8D%B7%E9%94%AE"><span class="toc-number">22.</span> <span class="toc-text">22、谷歌浏览器快捷键</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#23%E3%80%81git%E5%85%8B%E9%9A%86%E5%88%86%E6%94%AF"><span class="toc-number">23.</span> <span class="toc-text">23、git克隆分支</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#24%E3%80%81mysql%E6%9B%B4%E6%96%B0%E8%AF%AD%E5%8F%A5%E3%80%81%E6%96%B0%E5%A2%9E%E5%88%97%E3%80%81%E5%88%A0%E9%99%A4%E5%88%97"><span class="toc-number">24.</span> <span class="toc-text">24、mysql更新语句、新增列、删除列</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#25%E3%80%81%E5%88%A0%E9%99%A4%E6%8C%87%E5%AE%9A%E6%A0%BC%E5%BC%8F%E7%9A%84%E6%96%87%E4%BB%B6"><span class="toc-number">25.</span> <span class="toc-text">25、删除指定格式的文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#26%E3%80%81%E8%AE%A1%E7%AE%97%E6%96%87%E4%BB%B6%E6%80%BB%E6%95%B0"><span class="toc-number">26.</span> <span class="toc-text">26、计算文件总数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#27%E3%80%81%E8%AE%A1%E7%AE%97%E6%96%87%E4%BB%B6%E5%A4%B9%E5%A4%A7%E5%B0%8F"><span class="toc-number">27.</span> <span class="toc-text">27、计算文件夹大小</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#28%E3%80%81Django%E5%AE%9E%E7%8E%B0jsonp%E8%B7%A8%E5%9F%9F"><span class="toc-number">28.</span> <span class="toc-text">28、Django实现jsonp跨域</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#29%E3%80%81%E5%BE%AE%E4%BF%A1%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF"><span class="toc-number">29.</span> <span class="toc-text">29、微信获取用户信息</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#30%E3%80%81uwsgi%E5%88%9D%E5%A7%8B%E9%85%8D%E7%BD%AE%E9%97%AE%E9%A2%98"><span class="toc-number">30.</span> <span class="toc-text">30、uwsgi初始配置问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#31%E3%80%81django%E4%B8%ADdrf%E5%BA%8F%E5%88%97%E5%8C%96"><span class="toc-number">31.</span> <span class="toc-text">31、django中drf序列化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#32%E3%80%81%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%93"><span class="toc-number">32.</span> <span class="toc-text">32、第三方库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#33%E3%80%81requests%E8%AF%B7%E6%B1%82https%E6%90%BA%E5%B8%A6CA%E8%AF%81%E4%B9%A6"><span class="toc-number">33.</span> <span class="toc-text">33、requests请求https携带CA证书</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#34%E3%80%81django%E5%88%9B%E5%BB%BA%E7%BC%93%E5%AD%98%E5%91%BD%E4%BB%A4"><span class="toc-number">34.</span> <span class="toc-text">34、django创建缓存命令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#35%E3%80%81Django-%E6%9B%B4%E6%94%B9%E8%B6%85%E7%BA%A7%E7%94%A8%E6%88%B7%E5%AF%86%E7%A0%81"><span class="toc-number">35.</span> <span class="toc-text">35、Django 更改超级用户密码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#36%E3%80%81restframe%E4%BD%BF%E7%94%A8%E7%BC%93%E5%AD%98"><span class="toc-number">36.</span> <span class="toc-text">36、restframe使用缓存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#37%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">37.</span> <span class="toc-text">37、数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#38%E3%80%81linux%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="toc-number">38.</span> <span class="toc-text">38、linux常用命令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#39%E3%80%81xadmin%E7%A6%81%E6%AD%A2%E5%A2%9E%E5%8A%A0%E3%80%81%E5%88%A0%E9%99%A4"><span class="toc-number">39.</span> <span class="toc-text">39、xadmin禁止增加、删除</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#40%E3%80%81%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9B%B8%E5%87%8F"><span class="toc-number">40.</span> <span class="toc-text">40、时间格式字符串相减</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#41%E3%80%81%E6%98%BE%E7%A4%BA%E5%BE%AA%E7%8E%AF%E8%BF%9B%E5%BA%A6%E6%9D%A1"><span class="toc-number">41.</span> <span class="toc-text">41、显示循环进度条</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#42%E3%80%81%E6%8A%8A%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E5%AD%97%E5%85%B8%E8%BD%AC%E6%88%90csv%E6%96%87%E4%BB%B6"><span class="toc-number">42.</span> <span class="toc-text">42、把列表中的字典转成csv文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#43%E3%80%81windows%E6%B7%BB%E5%8A%A0%E5%8F%B3%E9%94%AE%E6%96%B0%E5%BB%BAMarkDown%E6%96%87%E4%BB%B6"><span class="toc-number">43.</span> <span class="toc-text">43、windows添加右键新建MarkDown文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#44%E3%80%81redis%E8%AE%BE%E7%BD%AE%E5%80%BC%E5%AE%9A%E6%97%B6%E8%BF%87%E6%9C%9F"><span class="toc-number">44.</span> <span class="toc-text">44、redis设置值定时过期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#45%E3%80%81linux%E6%A0%B9%E6%8D%AE%E7%AB%AF%E5%8F%A3%E6%9D%80%E8%BF%9B%E7%A8%8B"><span class="toc-number">45.</span> <span class="toc-text">45、linux根据端口杀进程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#46%E3%80%81%E7%9B%91%E6%8E%A7linux%E7%BD%91%E7%BB%9C%E6%B5%81%E9%87%8F"><span class="toc-number">46.</span> <span class="toc-text">46、监控linux网络流量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#47%E3%80%81win10%E6%B7%BB%E5%8A%A0%E5%8F%B3%E9%94%AE%E6%89%93%E5%BC%80cmd"><span class="toc-number">47.</span> <span class="toc-text">47、win10添加右键打开cmd</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#48%E3%80%81xadmin%E5%90%8E%E5%8F%B0%E6%97%A0%E6%B3%95%E6%98%BE%E7%A4%BA%E4%B8%8B%E6%8B%89%E6%A1%86%E5%AE%8C%E6%95%B4%E5%86%85%E5%AE%B9"><span class="toc-number">48.</span> <span class="toc-text">48、xadmin后台无法显示下拉框完整内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#49%E3%80%81xadmin%E5%8D%95%E7%82%B9%E7%99%BB%E5%BD%95"><span class="toc-number">49.</span> <span class="toc-text">49、xadmin单点登录</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#50%E3%80%81Django-restful-%E5%A4%9A%E4%B8%AAmodels%E6%95%B0%E6%8D%AE%E8%A1%A8%E5%BA%8F%E5%88%97%E5%8C%96%E5%90%88%E5%B9%B6%E8%BF%94%E5%9B%9E%EF%BC%88%E4%B8%80%E6%AC%A1%E8%AF%B7%E6%B1%82%E8%BF%94%E5%9B%9E%E5%A4%9A%E4%B8%AA%E5%BA%8F%E5%88%97%E5%8C%96%E5%99%A8%E6%95%B0%E6%8D%AE%EF%BC%89"><span class="toc-number">50.</span> <span class="toc-text">50、Django restful 多个models数据表序列化合并返回（一次请求返回多个序列化器数据）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#51%E3%80%81-Django%E5%BA%8F%E5%88%97%E5%8C%96%E5%99%A8%E8%BF%94%E5%9B%9E%E5%A4%96%E9%94%AE%E5%85%B3%E8%81%94%E6%95%B0%E6%8D%AE"><span class="toc-number">51.</span> <span class="toc-text">51、 Django序列化器返回外键关联数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#52%E3%80%81python-Django%E9%80%9A%E8%BF%87User-Agent%E5%88%A4%E6%96%AD%E8%AF%B7%E6%B1%82%E6%9D%A5%E6%BA%90%E6%98%AF%E5%BE%AE%E4%BF%A1%E6%89%AB%E4%B8%80%E6%89%AB%E6%88%96%E8%80%85%E6%98%AF%E6%94%AF%E4%BB%98%E5%AE%9D%E6%89%AB%E4%B8%80%E6%89%AB"><span class="toc-number">52.</span> <span class="toc-text">52、python Django通过User Agent判断请求来源是微信扫一扫或者是支付宝扫一扫</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#53%E3%80%81xadmin%E5%90%8E%E5%8F%B0%E9%9B%86%E6%88%90%E2%80%99%E5%AF%BC%E5%85%A5%E2%80%98%E6%8F%92%E4%BB%B6%EF%BC%8C%E5%AF%BC%E5%85%A5excel%E6%96%87%E4%BB%B6"><span class="toc-number">53.</span> <span class="toc-text">53、xadmin后台集成’导入‘插件，导入excel文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#54%E3%80%81Django%E4%B8%AD%E6%9F%A5%E6%89%BE%E4%BB%8A%E5%A4%A9%E8%BF%9B%E8%B4%A6%E9%87%91%E9%A2%9D"><span class="toc-number">54.</span> <span class="toc-text">54、Django中查找今天进账金额</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#55%E3%80%81%E5%88%A4%E6%96%AD%E6%98%AF%E4%BB%80%E4%B9%88%E7%B3%BB%E7%BB%9F"><span class="toc-number">55.</span> <span class="toc-text">55、判断是什么系统</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#56%E3%80%81sql%E6%9F%A5%E8%AF%A2"><span class="toc-number">56.</span> <span class="toc-text">56、sql查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#57%E3%80%81-xadmin%E5%90%8E%E5%8F%B0%E5%88%A0%E9%99%A4%E6%95%B0%E6%8D%AE%E5%87%BA%E7%8E%B0%E9%94%99%E8%AF%AF"><span class="toc-number">57.</span> <span class="toc-text">57、 xadmin后台删除数据出现错误</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#58%E3%80%81xdamin%E9%99%90%E5%88%B6%E7%94%A8%E6%88%B7%E7%82%B9%E5%87%BB"><span class="toc-number">58.</span> <span class="toc-text">58、xdamin限制用户点击</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#59%E3%80%81%E8%8E%B7%E5%8F%96%E5%85%AC%E4%BC%97%E5%8F%B7%E5%85%B3%E6%B3%A8url"><span class="toc-number">59.</span> <span class="toc-text">59、获取公众号关注url</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#60%E3%80%81xadmin%E5%90%8E%E5%8F%B0%E7%94%A8%E6%88%B7%E6%93%8D%E4%BD%9C%E8%A1%A8%E6%9D%83%E9%99%90"><span class="toc-number">60.</span> <span class="toc-text">60、xadmin后台用户操作表权限</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#61%E3%80%81%E4%BD%BF%E7%94%A8nginx%E9%83%A8%E7%BD%B2%E9%A1%B9%E7%9B%AE"><span class="toc-number">61.</span> <span class="toc-text">61、使用nginx部署项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#62%E3%80%81xadmin%E5%90%8E%E5%8F%B0%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E6%89%BE%E5%9B%9E%E5%AF%86%E7%A0%81"><span class="toc-number">62.</span> <span class="toc-text">62、xadmin后台发送邮件找回密码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#63%E3%80%81xadmin%E5%A4%96%E9%94%AE%E4%B8%8B%E6%8B%89%E6%A1%86%E6%B7%BB%E5%8A%A0%E8%BF%87%E6%BB%A4"><span class="toc-number">63.</span> <span class="toc-text">63、xadmin外键下拉框添加过滤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#64%E3%80%81xadmin%E5%8D%B3%E6%97%B6%E7%BC%96%E8%BE%91%E5%99%A8%E5%8E%BB%E6%8E%89%E7%A9%BA%E6%A0%87%E7%AD%BE"><span class="toc-number">64.</span> <span class="toc-text">64、xadmin即时编辑器去掉空标签</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#65%E3%80%81%E7%94%A8%E6%88%B7%E5%A2%9E%E5%8A%A0%E7%9A%84%E5%B0%8F%E7%BB%84%E4%BB%B6%EF%BC%8C%E8%AE%A9%E5%85%B6%E4%BB%96%E7%94%A8%E6%88%B7%E5%8F%AF%E8%A7%81"><span class="toc-number">65.</span> <span class="toc-text">65、用户增加的小组件，让其他用户可见</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#66%E3%80%81pip-install-uwsgi%E5%87%BA%E9%94%99"><span class="toc-number">66.</span> <span class="toc-text">66、pip install uwsgi出错</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#66%E3%80%81xadmin%E5%90%8E%E5%8F%B0%E5%8A%A0%E8%BD%BD%E6%95%B0%E6%8D%AE%E6%85%A2%EF%BC%8C%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="toc-number">67.</span> <span class="toc-text">66、xadmin后台加载数据慢，解决方案</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#67-%E3%80%81xadmin%E5%AF%BC%E5%87%BA%E6%8F%92%E4%BB%B6%E5%A4%84%E7%90%86%EF%BC%8C%E5%A2%9E%E5%8A%A0%E5%AF%BC%E5%87%BA%E5%8B%BE%E9%80%89%E6%95%B0%E6%8D%AE%E9%A1%B9"><span class="toc-number">68.</span> <span class="toc-text">67 、xadmin导出插件处理，增加导出勾选数据项</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1%E3%80%81%E5%8A%A0%E8%BD%BDjs%E6%96%87%E4%BB%B6"><span class="toc-number">68.0.1.</span> <span class="toc-text">1、加载js文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2%E3%80%81%E4%BF%AE%E6%94%B9export-py%EF%BC%8C%E5%90%8E%E7%AB%AF%E5%A4%84%E7%90%86%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6"><span class="toc-number">68.0.2.</span> <span class="toc-text">2、修改export.py，后端处理下载文件</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#68%E3%80%81%E4%BD%BF%E7%94%A8F%E6%9F%A5%E8%AF%A2%E6%9B%B4%E6%96%B0%E7%94%A8%E6%88%B7%E9%87%8D%E8%A6%81%E6%95%B0%E6%8D%AE"><span class="toc-number">69.</span> <span class="toc-text">68、使用F查询更新用户重要数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#69%E3%80%81%E6%97%A5%E5%BF%97%E8%BE%93%E5%87%BA%E6%A8%A1%E5%9D%97"><span class="toc-number">70.</span> <span class="toc-text">69、日志输出模块</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#70%E3%80%81%E5%AD%97%E5%85%B8%E6%8E%92%E5%BA%8F"><span class="toc-number">71.</span> <span class="toc-text">70、字典排序</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%B9%E5%88%97%E8%A1%A8%E4%B8%AD%E5%AD%97%E5%85%B8%E6%8C%89%E5%A4%9A%E4%B8%AA%E9%94%AE%E5%80%BC%E6%8E%92%E5%BA%8F"><span class="toc-number">71.1.</span> <span class="toc-text">对列表中字典按多个键值排序</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#71%E3%80%81%E6%97%B6%E9%97%B4%E6%A0%BC%E5%BC%8F%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BA%92%E8%BD%AC%E3%80%81%E6%AF%94%E8%BE%83%E5%A4%A7%E5%B0%8F"><span class="toc-number">72.</span> <span class="toc-text">71、时间格式与字符串互转、比较大小</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#72%E3%80%81python%E6%8A%8A%E2%80%99-u%E2%80%99%E5%BC%80%E5%A4%B4%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E4%B8%AD%E6%96%87"><span class="toc-number">73.</span> <span class="toc-text">72、python把’\u’开头的字符串转中文</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#73%E3%80%81django%E8%BF%9B%E8%A1%8C%E6%95%B0%E6%8D%AE%E8%BF%81%E7%A7%BB%E6%97%B6%E5%87%BA%E7%8E%B0No-changes-detected%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="toc-number">74.</span> <span class="toc-text">73、django进行数据迁移时出现No changes detected解决方案</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#74%E3%80%81ubuntu%E4%B8%8B%E8%BD%BD%E6%96%87%E4%BB%B6%E5%88%B0windows%E6%A1%8C%E9%9D%A2"><span class="toc-number">75.</span> <span class="toc-text">74、ubuntu下载文件到windows桌面</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#75%E3%80%81git%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E6%97%A5%E5%BF%97"><span class="toc-number">76.</span> <span class="toc-text">75、git查看提交日志</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#76%E3%80%81python%E7%BF%BB%E8%AF%91%E6%A8%A1%E5%9D%97"><span class="toc-number">77.</span> <span class="toc-text">76、python翻译模块</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#77%E3%80%81python%E5%AE%9E%E7%8E%B0%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E5%AD%97%E5%85%B8"><span class="toc-number">78.</span> <span class="toc-text">77、python实现字符串转字典</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#78%E3%80%81django-app-%E5%A6%82%E4%BD%95%E5%9C%A8%E5%90%8E%E5%8F%B0%E6%98%BE%E7%A4%BA%E4%B8%AD%E6%96%87%E5%90%8D"><span class="toc-number">79.</span> <span class="toc-text">78、django app 如何在后台显示中文名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#79%E3%80%81%E4%BD%BF%E7%94%A8python%E7%BB%99Excel%E6%8C%87%E5%AE%9A%E8%A1%8C%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE"><span class="toc-number">80.</span> <span class="toc-text">79、使用python给Excel指定行添加数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#80%E3%80%81python%E4%B8%AD%E7%9A%84format%E6%A0%BC%E5%BC%8F%E6%8B%BC%E6%8E%A5%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">81.</span> <span class="toc-text">80、python中的format格式拼接字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#81%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93inner-join%E3%80%81full-join%E3%80%81left-join%E3%80%81union%E3%80%81union-all%E5%8C%BA%E5%88%AB"><span class="toc-number">82.</span> <span class="toc-text">81、数据库inner join、full join、left join、union、union all区别</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#82%E3%80%81windows%E7%94%B5%E8%84%91%E6%9F%A5%E7%9C%8B%E7%AB%AF%E5%8F%A3%E4%BD%BF%E7%94%A8%E6%83%85%E5%86%B5"><span class="toc-number">83.</span> <span class="toc-text">82、windows电脑查看端口使用情况</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#83%E3%80%81Django%E7%94%9F%E6%88%90%E8%A1%A8%E5%92%8C%E5%8F%8D%E5%90%91%E7%94%9F%E6%88%90Model"><span class="toc-number">84.</span> <span class="toc-text">83、Django生成表和反向生成Model</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#84%E3%80%81windows%E7%9A%84hosts%E6%96%87%E4%BB%B6%E4%BD%8D%E7%BD%AE"><span class="toc-number">85.</span> <span class="toc-text">84、windows的hosts文件位置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#85%E3%80%81postgresql%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">86.</span> <span class="toc-text">85、postgresql数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#86%E3%80%81python%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%87%BA%E5%B8%A6%E9%A2%9C%E8%89%B2%E7%9A%84%E6%96%87%E5%AD%97%E6%96%B9%E6%B3%95"><span class="toc-number">87.</span> <span class="toc-text">86、python控制台输出带颜色的文字方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#87%E3%80%81%E6%8E%A7%E5%88%B6%E5%8F%B0%E8%BE%93%E5%87%BA%E7%99%BD%E8%89%B2%E6%96%B9%E6%A1%86"><span class="toc-number">88.</span> <span class="toc-text">87、控制台输出白色方框</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#88%E3%80%81xadmin%E5%90%8E%E5%8F%B0%E7%BC%96%E8%BE%91%E5%A4%9A%E5%AF%B9%E5%A4%9A%E5%AD%97%E6%AE%B5"><span class="toc-number">89.</span> <span class="toc-text">88、xadmin后台编辑多对多字段</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#89%E3%80%81Django%E5%90%8E%E5%8F%B0%E9%80%9A%E8%BF%87%E6%8C%89%E9%92%AE%E6%98%BE%E7%A4%BATextField%E5%AD%97%E6%AE%B5%E5%86%85%E5%AE%B9"><span class="toc-number">90.</span> <span class="toc-text">89、Django后台通过按钮显示TextField字段内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#90%E3%80%81%E5%89%8D%E7%AB%AF%E5%88%A4%E6%96%AD%E4%B8%8A%E4%BC%A0%E7%9A%84%E6%96%87%E4%BB%B6%E6%98%AF%E5%90%A6%E4%B8%BA%E5%9B%BE%E7%89%87%E4%B8%94%E9%99%90%E5%88%B6%E5%A4%A7%E5%B0%8F%E4%B8%BA300kB"><span class="toc-number">91.</span> <span class="toc-text">90、前端判断上传的文件是否为图片且限制大小为300kB</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#91%E3%80%81form%E8%A1%A8%E5%8D%95%E6%8F%90%E4%BA%A4%E5%89%8D%E9%AA%8C%E8%AF%81"><span class="toc-number">92.</span> <span class="toc-text">91、form表单提交前验证</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#92%E3%80%81%E6%9C%89%E5%BA%8F%E5%AD%97%E5%85%B8"><span class="toc-number">93.</span> <span class="toc-text">92、有序字典</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#93%E3%80%81Python%E5%9C%A8Windows%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%AE%9E%E7%8E%B0TTS%EF%BC%88%E6%96%87%E5%AD%97%E8%BD%AC%E8%AF%AD%E9%9F%B3%EF%BC%89"><span class="toc-number">94.</span> <span class="toc-text">93、Python在Windows系统下实现TTS（文字转语音）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#94%E3%80%81%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E9%97%B9%E9%92%9F"><span class="toc-number">95.</span> <span class="toc-text">94、定义一个简单闹钟</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#95%E3%80%81%E6%A0%B9%E6%8D%AE%E5%B9%B4%E6%9C%88%E8%8E%B7%E5%8F%96%E5%BD%93%E6%9C%88%E5%A4%A9%E6%95%B0"><span class="toc-number">96.</span> <span class="toc-text">95、根据年月获取当月天数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#96%E3%80%81Django%E8%8E%B7%E5%8F%96favicon-ico%E5%9B%BE%E6%A0%87"><span class="toc-number">97.</span> <span class="toc-text">96、Django获取favicon.ico图标</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#97%E3%80%81django-%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6%E9%85%8D%E7%BD%AE"><span class="toc-number">98.</span> <span class="toc-text">97、django 静态文件配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#98%E3%80%81django-JsonResponse%E8%BF%94%E5%9B%9E%E4%B8%AD%E6%96%87%E6%97%B6%E6%98%BE%E7%A4%BAunicode%E7%BC%96%E7%A0%81%EF%BC%88-u67e5-u8be2%EF%BC%89"><span class="toc-number">99.</span> <span class="toc-text">98、django JsonResponse返回中文时显示unicode编码（\u67e5\u8be2）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#99%E3%80%81%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4%E6%9C%88%E4%BB%BD%E7%9A%84%E9%A6%96%E6%97%A5%E4%B8%8E%E6%9C%80%E5%90%8E%E4%B8%80%E5%A4%A9"><span class="toc-number">100.</span> <span class="toc-text">99、获取当前时间月份的首日与最后一天</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#100%E3%80%81xadmin%E5%90%8E%E5%8F%B0%E6%89%B9%E9%87%8F%E6%93%8D%E4%BD%9C"><span class="toc-number">101.</span> <span class="toc-text">100、xadmin后台批量操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#101%E3%80%81xadmin%E8%BF%87%E6%BB%A4%E5%99%A8%E5%A4%96%E9%94%AE%E6%98%BE%E7%A4%BA%E7%89%B9%E5%AE%9A%E5%80%BC%EF%BC%88%E6%AF%94%E5%A6%82%E5%8F%AA%E8%83%BD%E8%BF%87%E6%BB%A4%E8%87%AA%E5%B7%B1%E4%B8%8E%E8%B6%85%E7%BA%A7%E7%AE%A1%E7%90%86%E5%91%98%E5%AE%9A%E4%B9%89%E7%9A%84%E6%95%B0%E6%8D%AE%EF%BC%89"><span class="toc-number">102.</span> <span class="toc-text">101、xadmin过滤器外键显示特定值（比如只能过滤自己与超级管理员定义的数据）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#102%E3%80%81Django%E4%B8%AD%E7%9A%84CSRF%E5%B1%80%E9%83%A8%E7%A6%81%E7%94%A8"><span class="toc-number">103.</span> <span class="toc-text">102、Django中的CSRF局部禁用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#103%E3%80%81%E4%BD%BF%E7%94%A8python%E5%91%BD%E4%BB%A4%E5%88%9B%E5%BB%BAdjango%E9%A1%B9%E7%9B%AE"><span class="toc-number">104.</span> <span class="toc-text">103、使用python命令创建django项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#104%E3%80%81-%E4%BA%8C%E7%BB%B4%E5%88%97%E8%A1%A8%E3%80%81%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E8%A1%8C%E6%B1%82%E5%92%8C%E4%B8%8E%E5%88%97%E6%B1%82%E5%92%8C"><span class="toc-number">105.</span> <span class="toc-text">104、 二维列表、二维数组行求和与列求和</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#105%E3%80%81%E8%8E%B7%E5%8F%96%E6%97%B6%E9%97%B4%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%9C%88%E4%BB%BD%E6%95%B0"><span class="toc-number">106.</span> <span class="toc-text">105、获取时间字符串的月份数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#106%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%8D%E8%B6%B3%E8%A1%A5%E9%9B%B6"><span class="toc-number">107.</span> <span class="toc-text">106、字符串不足补零</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#107%E3%80%81%E6%97%B6%E9%97%B4%E6%88%B3%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%97%A5%E6%9C%9F"><span class="toc-number">108.</span> <span class="toc-text">107、时间戳转字符串日期</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#108%E3%80%81%E5%88%97%E8%A1%A8%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">109.</span> <span class="toc-text">108、列表转字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#109%E3%80%81pip%E5%9B%BD%E5%86%85%E9%95%9C%E5%83%8F%E6%BA%90"><span class="toc-number">110.</span> <span class="toc-text">109、pip国内镜像源</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#110%E3%80%81python%E6%8A%8A%E6%97%B6%E9%97%B4%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E6%88%90%E5%88%9A%E5%88%9A%E3%80%811%E5%A4%A9%E5%89%8D%E3%80%813%E4%B8%AA%E6%9C%88%E5%89%8D%E3%80%811%E5%B9%B4%E5%89%8D"><span class="toc-number">111.</span> <span class="toc-text">110、python把时间字符串转换成刚刚、1天前、3个月前、1年前</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#111%E3%80%81Django%E4%BD%BF%E7%94%A8ORM%E6%89%A7%E8%A1%8Csql%E8%AF%AD%E5%8F%A5"><span class="toc-number">112.</span> <span class="toc-text">111、Django使用ORM执行sql语句</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#112%E3%80%81Django%E9%80%9A%E8%BF%87%E5%8F%98%E9%87%8F%E5%AD%97%E6%AE%B5%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE"><span class="toc-number">113.</span> <span class="toc-text">112、Django通过变量字段获取数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#113%E3%80%81Django%E6%9F%A5%E8%AF%A2%E5%8F%98%E9%87%8F%E5%AD%97%E6%AE%B5%E3%80%81%E6%9B%B4%E6%96%B0%E5%8F%98%E9%87%8F%E5%AD%97%E6%AE%B5"><span class="toc-number">114.</span> <span class="toc-text">113、Django查询变量字段、更新变量字段</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#114%E3%80%81vue%E7%94%9F%E6%88%90dist%E6%96%87%E4%BB%B6"><span class="toc-number">115.</span> <span class="toc-text">114、vue生成dist文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#115%E3%80%81python%E8%8E%B7%E5%8F%96%E7%94%B5%E8%84%91%E7%A3%81%E7%9B%98%E3%80%81CPU%E3%80%81%E5%86%85%E5%AD%98%E4%BD%BF%E7%94%A8%E6%83%85%E5%86%B5"><span class="toc-number">116.</span> <span class="toc-text">115、python获取电脑磁盘、CPU、内存使用情况</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#116%E3%80%81max%E6%AF%94%E8%BE%83%E5%AD%97%E5%85%B8%E5%88%97%E8%A1%A8"><span class="toc-number">117.</span> <span class="toc-text">116、max比较字典列表</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#117%E3%80%81django%E6%9F%A5%E8%AF%A2%E8%A1%A8%E7%9A%84%E5%85%B7%E4%BD%93%E5%AD%97%E6%AE%B5"><span class="toc-number">118.</span> <span class="toc-text">117、django查询表的具体字段</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#118%E3%80%81MySQL%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE%E6%B7%BB%E5%8A%A0%E8%87%AA%E5%A2%9E%E5%BA%8F%E5%8F%B7"><span class="toc-number">119.</span> <span class="toc-text">118、MySQL查询数据添加自增序号</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#119%E3%80%81MySQL-%E6%97%B6%E9%97%B4%E6%88%AA%E5%8F%96%E5%B9%B4%E6%9C%88%E6%97%A5"><span class="toc-number">120.</span> <span class="toc-text">119、MySQL 时间截取年月日</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#120%E3%80%81-%E5%88%97%E8%A1%A8%E5%8F%96%E5%A5%87%E6%95%B0%E4%B8%8B%E6%A0%87%E5%80%BC"><span class="toc-number">121.</span> <span class="toc-text">120、 列表取奇数下标值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#121%E3%80%81%E5%88%97%E8%A1%A8%E5%8F%96%E5%81%B6%E6%95%B0%E4%B8%8B%E6%A0%87%E5%80%BC"><span class="toc-number">122.</span> <span class="toc-text">121、列表取偶数下标值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#122%E3%80%81%E5%88%97%E8%A1%A8%E7%9B%B8%E5%90%8C%E5%85%83%E7%B4%A0%E5%88%86%E7%B1%BB%E3%80%81%E5%88%86%E7%BB%84"><span class="toc-number">123.</span> <span class="toc-text">122、列表相同元素分类、分组</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#123%E3%80%81ubuntu%E5%88%87%E6%8D%A2%E7%94%A8%E6%88%B7"><span class="toc-number">124.</span> <span class="toc-text">123、ubuntu切换用户</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#124%E3%80%81numpy%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E8%8E%B7%E5%8F%96%E6%9F%90%E4%B8%80%E5%88%97"><span class="toc-number">125.</span> <span class="toc-text">124、numpy二维数组获取某一列</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理">自制深度学习框架--实现Yolov5的推理</a><time datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理">自制深度学习框架--实现ResNet网络的推理</a><time datetime="2023-03-23T12:00:00.000Z" title="发表于 2023-03-23 20:00:00">2023-03-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程">自制深度学习框架--算子的执行流程</a><time datetime="2023-03-22T12:00:00.000Z" title="发表于 2023-03-22 20:00:00">2023-03-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系">自制深度学习框架--再探Tensor类并构建计算图的图关系</a><time datetime="2023-03-21T12:00:00.000Z" title="发表于 2023-03-21 20:00:00">2023-03-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现">自制深度学习框架--Im2Col原理与卷积层的实现</a><time datetime="2023-03-20T12:00:00.000Z" title="发表于 2023-03-20 20:00:00">2023-03-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By kiloGrand</div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>